2013-10-25 65 views
12

我有一个UITableview,它会加载所有不同大小的图像。当一个图像加载时,我需要更新特定的单元格,所以我想通过使用reloadRowsAtIndexPaths。但是,当我使用此方法时,它仍然为每个单元格调用heightForRowAtIndexPath方法。我认为reloadRowsAtIndexPaths的全部用途是它只会为您指定的特定行调用heightForRowAtIndexPath?iOS UITableView reloadRowsAtIndexPaths

任何想法为什么?

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

谢谢

+0

你尝试只使用.. [self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; –

+0

你的意思是不是将它包装在beginUpdates和endUpdates中?是的,我尝试过,它仍然试图重做每个单元格的高度。真的很烦人,因为我期待它为这个特定的单元格调用heightForRowAtIndexPath。 – Jesse

+0

我是否相信它应该只为这个单元调用heightForRowAtIndexPath?或者它是否再次为表格中的每个单元格调用此方法? – Jesse

回答

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

问题不是cellForRowAtIndexPath。这是事实,当我做任何类型的重新加载时,为UITableView中的每个单元格调用heightForRowAtIndexPath。我停止使用beginUpdates和endUpdates,但这仍然发生 – Jesse

+0

@Jesse您可能错过了我的观点。如果将单元的配置逻辑从'cellForRowAtIndexPath'移出单独的方法(您可以从'cellForRowAtIndexPath'调用,您可以通过直接调用方法来更新单元的配置,而不是使用'reloadRowsAtIndexPaths'(它间接调用'cellForRowAtIndexPath' ),因此绕过了重新计算内容大小(它间接调用'heightForRowAtIndexPath')的表视图的重载行为 –

+0

嗨蒂姆。我调用配置单元格方法,但我有自动布局约束冲突,它说我的单元格高度是300,而我的图片高度大于300.似乎调用配置单元格不会更新单元格的高度。是否有任何通知表视图来更新高度?我试图在再次调用configure单元之前手动调用heightForRow方法。 – Zhang