从indexPath获取UITableViewCell无效



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

[_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;
}

我认为这种方法不是更新单元格组件的好方法
如果你想将元素添加到单元格中,你可以用任何其他方法创建它,例如:

- tableView:willDisplayCell:forRowAtIndexPath:

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

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

 - tableView:didSelectRowAtIndexPath:

请参阅此

EXC_BAD_ACCESS高度为RowAtIndexPath iOS

尝试替换以下行

UITableViewCell *tempCell = [_tblBillHistory cellForRowAtIndexPath:indexPath];

有了这个

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

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

最新更新