我有一个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];