从indexPath获取UITableViewCell不工作

时间:2016-04-19 05:09:01

标签: ios objective-c uitableview

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

[_tblBillHistory beginUpdates];
[_tblBillHistory endUpdates];

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

Err-> EXC_BAD_ACCESS(代码= 2,地址= 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;
}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

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

- tableView:willDisplayCell:forRowAtIndexPath:
可以多次调用

heightForRowAtIndexPath (每个reloadData或动画块)。

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

 - tableView:didSelectRowAtIndexPath:

答案 2 :(得分:0)

尝试替换以下行

UITableViewCell *tempCell = [_tblBillHistory cellForRowAtIndexPath:indexPath];

这一个

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

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