tableview按钮没有使用正确的indexPath

时间:2013-07-29 09:46:45

标签: iphone ios button tags tableview

我的tableview中的每个单元格都有一个按钮。基本上我是这样保存单元格的indexPath in the button's tag in the cellForRowAtIndexPath`方法:

- (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重置时。 我在互联网上搜索了很多,但我已经坚持了几天这个问题。

3 个答案:

答案 0 :(得分:0)

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

如果你想把索引作为0,1,...... 6,7等等。

像这样使用,

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

但它不会重复使用细胞。

答案 1 :(得分: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;
}

感谢。

答案 2 :(得分:0)

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

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;

}