I have a UITableView populated with cells either containing just a text label or a text label and a UIImage. When the app is run, images are downloaded from my server and stored on the device locally. Every time the app is run, the app checks for new images and downloads them. In my cellForRowAtIndexPath
function, I load the image from the device storage on a background thread then update the UI on the main thread.
I don't know if I am doing this right but here is my code for displaying images inside my cellForRowAtIndexPath
function:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
for image in self.postsImages {
if ((image as! NSArray)[0] as! Int == postIDCurrent) {
// there is an image associated with this post
let postImage = UIImage(contentsOfFile: (currentArray.lastObject as? String)!)
dispatch_async(dispatch_get_main_queue(), {
cell.postImageView.image = postImage
cell.postImageView.clipsToBounds = true
cell.postImageView.userInteractionEnabled = true
cell.postImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PostsTableViewController.imageTapped(_:))))
})
}
}
})
The for
and the if
statements both just really check if there is an image associated with the current cell we are going to display.
I'm sorry if I'm not providing sufficient information. If you need anything more, please comment.
Anyways, my question is, what am I doing wrong with this code which causes my UITableView to scroll very slow?