NeoWang
NeoWang

Reputation: 18583

How to update a UITableViewCell height after tableView is loaded

The custom UITableViewCell includes an image which is loaded asynchronously from web, and its size/aspectRatio is not known beforehand, so there is no way to return the accurate cell height in tableView:heightForRowAtIndexPath:.

I can return an estimated height in the delegate method, but need to inform the tableView of the cell height change when the image is loaded. How should I do that?

  1. reloadData would be too heavy-weight, considering each time an image is loaded, the whole tableView is refreshed.
  2. reloadRowsAtIndexPaths:withRowAnimation: is not good, either. As the cell itself doesn't change, just its height needs to be updated.

Upvotes: 4

Views: 2942

Answers (3)

Aditya Mathur
Aditya Mathur

Reputation: 1185

You can very easily reload the table by calling

[tableview reloadData];

From this answer:

The UITableView class never loads cells until they're about to appear onscreen, even when you call reloadData.

Hence only visible cells will be reloaded.

Upvotes: 1

Reload the visible table cells - is the best one . Otherwise every time you need to reload the table view entirely, and then it will put you on first cell. then programatically you should scroll down the particular visited cell.it is pain thing.Not a good user interface as well.

    [YourTableView reloadRowsAtIndexPaths:[YourTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];

Reload only download completion cells

    [YourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:YourImageCompletelyDownloadRow inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];

Upvotes: 1

Kumar KL
Kumar KL

Reputation: 15335

Hopefully, you don't have any other option except [self.tableView reloadData];, anyhow it doesn't reload all data - its just reloads the visible cells .

Upvotes: 3

Related Questions