简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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