繁体   English   中英

iOS UITableView reloadRowsAtIndexPaths

[英]iOS UITableView reloadRowsAtIndexPaths

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

知道为什么吗?

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

谢谢

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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM