I am displaying some text and images in a UITableView
. The image first gets downloaded. Since before the image gets downloaded, I don't know the size of image, so I initially put a UIImageView
of some fixed size. And when the image is downloaded, I resize the UIImageView
.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Download image
dispatch_async(dispatch_get_main_queue(), ^{
// UIImageView resizing
});
});
All this happens in cellForRowAtIndexPath
.
The issues I am facing here are :
1. How do I update the height of cell ? Considering that there can be many images in a single cell. So I need to change the position of bottom image when above one downloads.
2. I tried using UITableView
beginUpdates
and endUpdates
, but that scrolls to the top of cell giving a poor user experience.
This is how the UI looks like on reloadData. There are 5 images to be downloaded : UI experience after UITableView reloadData
Short Answer
estimatedRowHeight
UITableViewCell
once returned bydequeueReusableCellWithIdentifier
will not work with cached cellsreloadRowsAtIndexPaths
NSFetchedResultsController
boilerplate code can do all the UI work.In Details
No unexpected scroll, only updates images as they come:
UITableView
will not scrollUITableView
will not scrollUITableView
will only scroll when the cell is in plain sight, and requires more space than available.Let
UITableViewAutomaticDimension
do the hard workYou need to tell Cocoa Touch that the cell is stale, so that it will trigger a new
dequeueReusableCellWithIdentifier
, to which you are going to return a cell with the proper height.Short of reloading the entire table view or one of its section, and assuming that your indexes are stable, invoke
-tableView:reloadRows:at:with:
passing the indexPath of the cell that just changed, and a.fade
animation.Code:
Use
URLSession
. When an image becomes available, firereloadRows:at:with
:Once in the cache,
cellForRow
merely reads into it from the UI thread:Example: fetch a random set of images from *Wikipedia*
► Find this solution on GitHub and additional details on Swift Recipes.