iOS中的EXC_BAD_ACCESS为heightForRowAtIndexPath

And*_*ers 26 objective-c uitableview ios

我正在开发一个应用程序,我有一个UITableViewCell的自定义子类.我想根据里面的文字使单元格的高度动态化.我尝试在我的heightForRowAtIndexPath方法中做到这一点.但我有一些问题,以下代码导致和EXC_BAD_ACCESS(代码= 2地址= 0xb7ffffcc)错误.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    PostCell *cell = (PostCell *)[tableView cellForRowAtIndexPath:indexPath];

    CGSize postTextSize;
    if (![cell.postText.text isEqualToString:@""]) {
        postTextSize = [cell.postText.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(cell.postText.frame.size.width, 200)];
    } else {
        postTextSize = CGSizeMake(cell.postText.frame.size.width, 40);
    }

    return postTextSize.height + cell.userName.frame.size.height + 10;
}
Run Code Online (Sandbox Code Playgroud)

如果我改为:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    return 100;
}
Run Code Online (Sandbox Code Playgroud)

有用.

它似乎在这条线上崩溃:PostCell *cell = (PostCell *)[tableView cellForRowAtIndexPath:indexPath];,我不知道为什么.我试图建立干净并重置模拟器,但都没有工作.任何想法为什么会这样?

Mar*_*n R 54

我试图重现这个问题.事实证明,调用cellForRowAtIndexPath:内部heightForRowAtIndexPath导致heightForRowAtIndexPath递归调用.以下是3个递归步骤之后的堆栈回溯的摘录:

frame #0: 0x000042d0 DocInteraction`-[DITableViewController tableView:heightForRowAtIndexPath:] + 48 at DITableViewController.m:262
frame #1: 0x0054f688 UIKit`-[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 3437
frame #2: 0x0055040f UIKit`-[UITableViewRowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 144
frame #3: 0x00551889 UIKit`-[UITableViewRowData numberOfRows] + 137
frame #4: 0x00553dac UIKit`-[UITableViewRowData globalRowsInRect:] + 42
frame #5: 0x003f82eb UIKit`-[UITableView(_UITableViewPrivate) _visibleGlobalRowsInRect:] + 177
frame #6: 0x004001e6 UIKit`-[UITableView cellForRowAtIndexPath:] + 113
frame #7: 0x000042f2 DocInteraction`-[DITableViewController tableView:heightForRowAtIndexPath:] + 82 at DITableViewController.m:262
frame #8: 0x0054f688 UIKit`-[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 3437
frame #9: 0x0055040f UIKit`-[UITableViewRowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 144
frame #10: 0x00551889 UIKit`-[UITableViewRowData numberOfRows] + 137
frame #11: 0x00553dac UIKit`-[UITableViewRowData globalRowsInRect:] + 42
frame #12: 0x003f82eb UIKit`-[UITableView(_UITableViewPrivate) _visibleGlobalRowsInRect:] + 177
frame #13: 0x004001e6 UIKit`-[UITableView cellForRowAtIndexPath:] + 113
frame #14: 0x000042f2 DocInteraction`-[DITableViewController tableView:heightForRowAtIndexPath:] + 82 at DITableViewController.m:262
frame #15: 0x0054f688 UIKit`-[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 3437
frame #16: 0x0055040f UIKit`-[UITableViewRowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 144
frame #17: 0x00551889 UIKit`-[UITableViewRowData numberOfRows] + 137
frame #18: 0x003ff66d UIKit`-[UITableView noteNumberOfRowsChanged] + 119
frame #19: 0x003ff167 UIKit`-[UITableView reloadData] + 764
Run Code Online (Sandbox Code Playgroud)

最后程序崩溃了.在我的模拟器上,当后面的迹线深约57000级时会发生这种情况.


老答案(没错,但不解释EXC_BAD_ACCESS):

问题是

[tableView cellForRowAtIndexPath:indexPath]
Run Code Online (Sandbox Code Playgroud)

返回nil当前不可见的行.表视图仅分配显示当前可见行所需的许多单元格.滚动表视图时,将重用单元格.

但是在显示任何行之前heightForRowAtIndexPath调用表视图的所有单元格.

因此,您不应该从表格视图单元格中获取文本来计算高度heightForRowAtIndexPath.您应该从数据源获取文本.


fen*_*ngd 7

tableView heightForRowAtIndexPath之前调用cellForRowAtIndexPath,在显示单元格之前,需要首先计算高度.

你应该text从你的数据源获得,而不是从cell