I have a UITableView with a few different sections. One section contains cells that will resize as a user types text into a UITextView. Another section contains cells that render HTML content, for which calculating the height is relatively expensive.
Right now when the user types into the UITextView, in order to get the table view to update the height of the cell, I call
[self.tableView beginUpdates];
[self.tableView endUpdates];
However, this causes the table to recalculate the height of every cell in the table, when I really only need to update the single cell that was typed into. Not only that, but instead of recalculating the estimated height using tableView:estimatedHeightForRowAtIndexPath:
, it calls tableView:heightForRowAtIndexPath:
for every cell, even those not being displayed.
Is there any way to ask the table view to update just the height of a single cell, without doing all of this unnecessary work?
Update
I'm still looking for a solution to this. As suggested, I've tried using reloadRowsAtIndexPaths:
, but it doesn't look like this will work. Calling reloadRowsAtIndexPaths:
with even a single row will still cause heightForRowAtIndexPath:
to be called for every row, even though cellForRowAtIndexPath:
will only be called for the row you requested. In fact, it looks like any time a row is inserted, deleted, or reloaded, heightForRowAtIndexPath:
is called for every row in the table cell.
I've also tried putting code in willDisplayCell:forRowAtIndexPath:
to calculate the height just before a cell is going to appear. In order for this to work, I would need to force the table view to re-request the height for the row after I do the calculation. Unfortunately, calling [self.tableView beginUpdates]; [self.tableView endUpdates];
from willDisplayCell:forRowAtIndexPath:
causes an index out of bounds exception deep in UITableView's internal code. I guess they don't expect us to do this.
I can't help but feel like it's a bug in the SDK that in response to [self.tableView endUpdates]
it doesn't call estimatedHeightForRowAtIndexPath:
for cells that aren't visible, but I'm still trying to find some kind of workaround. Any help is appreciated.
reloadRowsAtIndexPaths:
? – WainreloadRowsAtIndexPaths:
also triggers the height of all rows to be recalculated. I don't think it's really what I want anyway, since it will create a new cell instead of resizing the existing one. – Chris Vasselli