I know that there is similar question: Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling
But I do not only load images from URL. I have them, perform some actions on them, and then assign them to UIImageView
WITHIN UITableViewCell
.
This is how I do this:
Within
cellForRowAtIndexPath:
cell.configureCellWithComment(comment)
These are methods inside my cell:
func configureCellWithComment(comment: WLComment) { if let attachmentUrl = comment.attachmentUrl, let fileName = NSURL(string: attachmentUrl)?.lastPathComponent { if !NSFileManager.defaultManager().fileExistsAtPath(comment.destinationPathForAttachment().URLByAppendingPathComponent(fileName).path!) { WLFileClient.sharedClient().downloadFileFrom(attachmentUrl, destinationPath: comment.destinationPathForAttachment(), fileName: fileName, progress: nil, completionBlock: { error in self.attachImageToImageView() }) } else { attachImageToImageView() } } }
Private method:
private func attachImageToImageView() {
if let attachmentUrl = comment.attachmentUrl, let fileName = NSURL(string: attachmentUrl)?.lastPathComponent {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let image = WLFileClient.sharedClient().previewImageForFileAtUrl(self.comment.destinationPathForAttachment().URLByAppendingPathComponent(fileName))
dispatch_async(dispatch_get_main_queue(), {
self.previewAttachmentButton.enabled = true
self.attachmentPreviewImageView.image = image
});
});
}
}
What should I do to fix this?