Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITableView reloadRowsAtIndexPaths

I have a UITableview that lazy loads images of all different sizes. When an image loads, I need to update the specific cell, so I figured out I need to use reloadRowsAtIndexPaths. But when I use this method, it still calls the heightForRowAtIndexPath method for every single cell. I thought the whole purpose of reloadRowsAtIndexPaths is that it will only call heightForRowAtIndexPath for the specific row you specify?

Any idea why?

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

Thank You

like image 333
Jesse Avatar asked Oct 25 '13 17:10

Jesse


1 Answers

endUpdates triggers a content size recalculation, which requires heightForRowAtIndexPath. That's just how it works.

If it's a problem, you could pull your cell configuration logic outside of cellForRowAtIndexPath and reconfigure the cell directly without going through reloadRowsAtIndexPaths. Here is a basic outline for what this could look like:

- (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
}

Then, wherever you're currently calling reloadRowsAtIndexPaths, you do this instead and heightForRowAtIndexPath won't be called:

UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
[self tableView:self.messageTableView configureCell:cell atIndexPath:indexPath];
like image 63
Timothy Moose Avatar answered Sep 17 '22 15:09

Timothy Moose