【问题标题】:iOS UITableView reloadRowsAtIndexPathsiOS UITableView reloadRowsAtIndexPaths
【发布时间】:2013-11-04 22:43:43
【问题描述】:

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

知道为什么吗?

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

谢谢

【问题讨论】:

  • 你试过只使用.. [self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  • 您的意思是不要将其包装在 beginUpdates 和 endUpdates 中?是的,我试过了,它仍然试图重做每个单元格的高度。真的很烦人,因为我希望它只为这个特定的单元格调用 heightForRowAtIndexPath。
  • 我相信它应该只为这个单元格调用 heightForRowAtIndexPath 是否正确?还是对表格中的每个单元格再次调用此方法?
  • 我认为它应该只要求您重新加载的行。我不知道为什么它要求所有可见的行。这可能取决于您如何显示您的单元格。尝试不使用 dequeueReusableCellWithIdentifier。
  • 如何创建没有 dequeueReusableCellWithIdentifier 的单元格?所以我确定的是,我的应用程序中 reloadData 和 reloadRowsAtIndexPaths 之间的唯一区别是 reloadData 为每个可见单元格调用 cellForRowIndex,而 reloadRowsAtIndexPaths 仅为一个单元格调用 cellForRowIndex。但他们仍然为表中的每个单元格调用 heightForRowAtIndexPath uggh

标签: ios uitableview


【解决方案1】:

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

【讨论】:

  • 问题不在于 cellForRowAtIndexPath。事实上,当我执行任何类型的重新加载时,UITableView 中的每个单元格都会调用 heightForRowAtIndexPath。我停止使用 beginUpdates 和 endUpdates,但这仍然发生
  • @Jesse 你可能错过了我的意思。如果您将单元格的配置逻辑从cellForRowAtIndexPath 移出到一个独立的方法(您将从cellForRowAtIndexPath 调用,您可以通过直接调用该方法而不是使用reloadRowsAtIndexPaths(间接调用cellForRowAtIndexPath)来更新单元格的配置) 从而绕过表视图的重新加载行为,该行为涉及重新计算内容大小(间接调用heightForRowAtIndexPath)。
  • 嘿蒂姆。我调用了配置单元格方法,但我遇到了自动布局约束冲突,它说我的单元格高度是 300,而我的图片高度大于 300。似乎调用配置单元格不会更新单元格的高度。有没有通知表格视图更新高度?我尝试在再次调用配置单元之前手动调用 heightForRow 方法。
  • @Zhang 您需要重新加载行才能让表格视图更新它们的高度。
  • 但是如果我调用 reloadRowsAtIndexPath 它会使应用程序崩溃,如上所述的问题?
猜你喜欢
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 2023-04-02
  • 2015-02-06
相关资源
最近更新 更多