Cherif
Cherif

Reputation: 5383

How to refresh an UIImage in an UITableViewCell?

This is how I create my cells :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("FbFriendCell") as! FbFriendCell
        cell.initialize()
        return cell  
}

Very simple.

In the initialize() function, I download an image and I put it in an UIImageView :

let theImageView = UIImageView(image: userPhoto)
theImageView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
self.addSubview(theImageView)
theImageView.image = userPhoto
println("The image has been set !")

The problem is that image only appears several seconds after this log message :

println("The image has been set !")

How can I force the image to reload ?

* READ THIS * => The issue could be strongly related to the fact that the image is inside an UITableViewCell !

Upvotes: 2

Views: 1442

Answers (3)

Nathan
Nathan

Reputation: 12294

There is a nicer way to access the UI thread in Swift3.

DispatchQueue.main.async() {

}

Some people say this is slower than dispatch_async, but I have never seen any difference in practical use.

Upvotes: 1

Sayaki
Sayaki

Reputation: 789

I think you download your image asynchronous. Your log is appearing when program reach println("The image has been set !") line but in fact your image is not downloaded yet. You should move your log to to the end of your download callback and then log should appear just in time when image will appear.

Alternatively this could happen because you didn't updated your UIImageView in UI thread. You can do this like that:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
        //Set image to UIImageView
})

Upvotes: 2

Leo
Leo

Reputation: 24714

Make sure you update the image on main queue

 dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //Update UI
 })

I have this issue before,and this solve my problem.Just try

Upvotes: 6

Related Questions