I have a UITableView
, I use the cellForRowAtIndexPath:
function to download an image for a specific cell. The image download is performed asynchronously and finishes after a short amount of time and calls a completion block after finishing. I set the cell content to the downloaded image inside the completion block, but it doesn't get updated until after the user finishes scrolling. I know that the reason is because the code should be running in NSRunLoopCommonModes
, but I don't know how to do that in this case, any help is appreciated.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let dequeued = self.tableView.dequeueReusableCellWithIdentifier(imageFileCellIdentifier)
let cell = dequeued as! ImageTableViewCell
source.FetchImage(imageLocation, completion: { (image) in
dispatch_async(dispatch_get_main_queue(), {
cell.previewImage.image = image
cell.previewImage.contentMode = .ScaleAspectFill
}
})
}