How do I tell my UITableViewCell to "auto-resize" when its content changes? How do I tell my UITableViewCell to "auto-resize" when its content changes? ios ios

How do I tell my UITableViewCell to "auto-resize" when its content changes?


There is a documented way to do this. See UITableView.beginUpdates() documentation:

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

So, the correct solution is:

tableView.beginUpdates()tableView.endUpdates()

Also note that there is a feature that is not documented - you can add a completion handler for the update animation here, too:

tableView.beginUpdates()CATransaction.setCompletionBlock {   // this will be called when the update animation ends}tableView.endUpdates()

However, tread lightly, it's not documented (but it works because UITableView uses a CATransaction for the animation).


I've found the best way to get it to check heights is to call, after whatever text change has been made, in order:

self.tableView.beginUpdates()self.tableView.endUpdates()

This causes the tableView to check heights for all visible cells and it will cause changes to be made as needed.


I think the simplest solution is to reload that specific cell. For example:

- (void)yourDelegateMethodOfCell:(UITableViewCell *)cell {    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];    //If cell is not visible then indexPath will be nil so,    if (indexPath) {        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    }}


matomo