meow
meow

Reputation: 28164

Changes to UIButton Does Not Update Cell Until Scrolled

I solved this using UIButtonState (duhz), but I am curious why exactly this did not work in the first place.

I defined the "normal" icons in the storyboard, then configured it to show the "highlighted" icon when the object is set. However, when the table first loaded, I did not see the updated icon.

But after the table was scrolled, the highlighted icons showed up. What is happening here?

class TweetCell: UITableViewCell {


    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var tweetLabel: UILabel!
    @IBOutlet weak var timeStampLabel: UILabel!
    @IBOutlet weak var favButton: UIButton!
    @IBOutlet weak var replyButton: UIButton!
    @IBOutlet weak var retweetButton: UIButton!

    var tweet: Tweet!{
        didSet {
            profileImage.setImageWithURL(NSURL(string:tweet.user.profile_image_url))
            nameLabel.text = tweet.user.name
            tweetLabel.text = tweet.text

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "h:mm"
            let str = dateFormatter.stringFromDate(tweet.createdAt)
            timeStampLabel.text = str

            if (tweet.favorited) {
                favButton.imageView?.image = UIImage(named: "star_highlighted.png")
            } else {

            }

            if (tweet.retweeted) {
                retweetButton.imageView?.image = UIImage(named: "retweet_highlighted.png")
            } else {

            }

        }
    }

// Before Scrolling enter image description here

// After Scrolling enter image description here

Upvotes: 1

Views: 69

Answers (2)

vineeth s thayyil
vineeth s thayyil

Reputation: 478

This is happening because the tableview is reusing cells which have the highlighted image view. To solve this issue you need to set the unhighlighted image in the else section.

if (tweet.favorited) {
                favButton.imageView?.image = UIImage(named: "star_highlighted.png")
            } else {
    favButton.imageView?.image = UIImage(named: "star_unhighlighted.png")
            }

Upvotes: 2

Rahul Shirphule
Rahul Shirphule

Reputation: 999

not sure for cause but you can use this to fix,I know you already fix it but still posting the way to fix this issue.

[cell performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];

Upvotes: 1

Related Questions