【问题标题】:tableview button not using correct indexPathtableview 按钮未使用正确的 indexPath
【发布时间】:2013-07-29 09:46:45
【问题描述】:

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 会重置。 我在互联网上搜索了很多,但我被这个问题困扰了好几天。

【问题讨论】:

  • 你怎么知道 indexpath 改变了?添加代码。使用故事板?
  • 是的,我正在使用情节提要,并且我知道 indexPath 不是 NSLogs 应该有的(它应该显示 6 时显示 indexPath 0,应该显示 7 时显示 1,等等. 另外,我正在使用原型单元格。

标签: iphone ios button tags tableview


【解决方案1】:

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

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

这样使用,

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

但它不会重复使用单元格。

【讨论】:

  • 表格是空的。我通过日志知道,它正在加载正确数量的单元格。
  • 我真的需要找到解决方案。你能复习一下吗?
  • 你到底想做什么??
  • 按钮在原型单元格中创建
【解决方案2】:

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

- (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;
}

谢谢。

【讨论】:

    【解决方案3】:

    自定义表格视图单元格是解决此问题的最佳方法,这里我们需要创建不同的自定义表格视图单元格控制器并在 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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多