iOS UITableView reloadRowsAtIndexPaths

时间:2013-10-25 17:39:03

标签: ios uitableview

我有一个UITableview,懒惰加载所有不同大小的图像。加载图像时,我需要更新特定的单元格,所以我想出了我需要使用reloadRowsAtIndexPaths。但是当我使用这个方法时,它仍然为每个单元格调用heightForRowAtIndexPath方法。我认为reloadRowsAtIndexPaths的全部目的是它只会为你指定的特定行调用heightForRowAtIndexPath吗?

知道为什么吗?

[self.messageTableView beginUpdates];
[self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.messageTableView endUpdates];

谢谢

1 个答案:

答案 0 :(得分:7)

endUpdates触发内容大小重新计算,需要heightForRowAtIndexPath。这就是它的工作原理。

如果这是一个问题,您可以将您的单元配置逻辑拉出cellForRowAtIndexPath,并直接重新配置单元格而不通过reloadRowsAtIndexPaths。以下是这可能是什么的基本概要:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = ...;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    [self tableView:tableView configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    //cell configuration logic here
}

然后,无论您当前正在呼叫reloadRowsAtIndexPaths,都会执行此操作而不会调用heightForRowAtIndexPath

UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
[self tableView:self.messageTableView configureCell:cell atIndexPath:indexPath];
相关问题