-1

我的每个单元格中都有一个按钮tableview。基本上我正在保存单元格的indexPath in the button's tag in thecellForRowAtIndexPath` 方法,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIButton *expandPhoto = (UIButton *)[cell viewWithTag:101];
    expandPhoto.tag = indexPath.row;
    [expandPhoto addTarget:self action:@selector(expand:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

问题是,当我向下滚动按钮的indexPath重置时。我在互联网上搜索了很多,但我被这个问题困扰了好几天。

4

3 回答 3

0

您正在使用static NSString *CellIdentifier = @"Cell";它将重用单元格,因此当它应该显示 6 时,您将得到单元格索引为 0,当它应该显示 7 时显示为 1,等等。

如果要获取索引为 0,1,....6,7 等。

像这样使用,

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];

但它不会重用单元格。

于 2013-07-29T10:08:05.620 回答
0

我使用以下代码解决了我的问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIButton *expandPhoto = (UIButton *)[cell viewWithTag:101];
    [expandPhoto setTag:indexPath.row];
    [expandPhoto addTarget:self action:@selector(expand:) forControlEvents:UIControlEventTouchUpInside];


    return cell;
}

- (IBAction)expand:(id)sender {
    UIButton *btn = (UIButton*) sender;
    UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    int row = indexPath.row;
}

谢谢。

于 2013-07-29T12:22:46.947 回答
0

自定义表格视图单元格是解决此问题的最佳方法,这里我们需要创建不同的自定义表格视图单元格控制器并在 cellForAtIndexPath 表格视图数据源中使用它:

CustomTableViewCell.h

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, readonly) UILabel  *tblViewLabel;
@property (nonatomic, readonly) UIButton *tblViewBtn;
@end

CustomTableViewCell.m

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell
@synthesize  tblViewLabel = _tblViewLabel;
@synthesize  tblViewBtn = _tblViewBtn;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString 
*)reuseIdentifier

{

  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

 if (self) {
    // Initialization code


   _tblViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 50, 30)];
    [self.contentView addSubview:_tblViewLabel];
    [_tblViewLabel release];

    _tblViewBtn         = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _tblViewBtn.frame   = CGRectMake(100, 5, 200, 20);
    [_tblViewBtn.titleLabel setText:[NSString stringWithFormat:@"%d",self.tag]];

    [_tblViewBtn addTarget:self action:@selector(tblBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_tblViewBtn];
    [_tblViewBtn release];

}
return self;

}


- (void)tblBtnPressed :(UIButton *)btn {

  NSLog(@"Here %d button is pressed.",btn.tag);

}



- (void)dealloc {
[_tblViewLabel release];
[_tblViewBtn release];
[super dealloc];

}
@end

//------------------------------------//

Here In Your Previous controller's cellForRowAtIndexPath datasource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyIdentifier";
CustomTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier] ;
if (cell == nil){
    cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyIdentifier];

}

cell.tblViewLabel.text =[NSString stringWithFormat:@"%d",indexPath.row];

[cell.tblViewBtn setTitle:[NSString stringWithFormat:@"%d",indexPath.row] forState:UIControlStateNormal];
cell.tblViewBtn.tag = indexPath.row;


return cell;

}
于 2013-07-29T12:30:05.637 回答