14
votes

I am working on a twitter-like client, and there is a message list view implemented by UITableView. For each cell, it may need to load a dynamic size image, and the image is loaded async. So it size of the image is unknown when the table view loaded.

Is it possible to update the uitableviewcell's height after the image is loaded.

From my knowledge, the only way to update the height is in this method: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath But it is just called when loading data at first time. This is reasonable from performance consideration. Anyhow, is there a way to change the height later?

Thanks for any information!

Finally, I used "reloadRowsAtIndexPaths" to update particular rows, you can try this for hidden cells.

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];

But, the cell shakes when updating the visible cells's height. So the it's better to have the cell height calculated before.

3
the second parameter of -reloadRowsAtIndexPaths:withRowAnimation: is not a BOOL; it is a UITableViewRowAnimation typeuser102008

3 Answers

27
votes

Try calling an empty Update block:

[self.tableView beginUpdates];
[self.tableView endUpdates];

This seems to work for visible cells. I'm still struggling with hidden cells.

5
votes

heightForRowAtIndexPath: is actually called again when you perform a reloadData (you could set a breakpoint to verify). All you need to do is put the conditional logic you need in there.

-4
votes
[tableView reloadData];

will do what u want. It will reload entire table datasource. So u can set rowHeightaccordingly