【问题标题】:Get UITableViewCell from indexPath not Working从 indexPath 获取 UITableViewCell 不起作用
【发布时间】:2016-04-19 13:33:22
【问题描述】:

我试图在更新单元格高度的同时更新单元格项目,但它不起作用。

[_tblBillHistory beginUpdates];
[_tblBillHistory endUpdates];

我用来更新单元格高度的这些方法

Err-> EXC_BAD_ACCESS(code=2,address=0x7fff5b4daf98) 获取 cellForRowAtIndexPath 方法的此错误消息。 Nslog 还正确打印所选行。任何想法???谢谢。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if((selectedPath!=nil ) & ([indexPath isEqual:selectedPath])) {
        NSLog(@"%ld",indexPath.row);
        UITableViewCell *tempCell = [_tblBillHistory cellForRowAtIndexPath:indexPath];

        UIStackView *stkItems=(UIStackView *)[tempCell viewWithTag:8];

        for(int i=0;i<20;i++)
        {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:@"test button" forState:UIControlStateNormal];
            [stkItems addArrangedSubview:btn];
        }

        return 700;
    }

    return 250;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    我认为这种方法不是更新单元格组件的好地方。
    如果您想向单元格添加元素,您可以通过任何其他方法创建它,例如:

    - tableView:willDisplayCell:forRowAtIndexPath:
    

    heightForRowAtIndexPath 可能会被多次调用(每个 reloadData 或动画块)。

    你应该划分你的代码,在 heightForRowAtIndexPath 你应该只计算高度而不请求单元格(因为 tableView 在这个方法中没有单元格)。 向可以在 didSelectMethod 中创建的单元格添加或删除元素(但更好的解决方案将此代码移动到单元格或为选定的单元格创建自定义类):

     - tableView:didSelectRowAtIndexPath:
    

    【讨论】:

    • 是的。我认为这就是问题所在。
    【解决方案2】:

    【讨论】:

    • 我想我找到了解决方案。我必须在调用 beginUpdates 和 endUpadates 方法之前调用 cellForRowAtIndexPath 方法。但我不知道是什么原因。
    【解决方案3】:

    尝试替换以下行

    UITableViewCell *tempCell = [_tblBillHistory cellForRowAtIndexPath:indexPath];
    

    这个

    UITableViewCell *tempCell = [self tableView:_tblBillHistory cellForRowAtIndexPath:indexPath];
    

    这将停止递归调用,因为这将调用 cellForRowAtIndexPath 作为函数而不是表视图的数据源......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2014-11-10
      • 2011-01-23
      • 1970-01-01
      • 2014-07-27
      • 2012-10-24
      • 2014-09-25
      相关资源
      最近更新 更多