iOS custom UITableViewCell set UIButton title label text

I am making a chatting application. In my datasource method, I dequeue a cell and pass a Core Data Entity to initialize it.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Post *post = self.posts[indexPath.row];

    AbstractPostCell *cell;
    if ([post.type isEqualToString:@"text"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:CELL_POST_LIST_TEXT forIndexPath:indexPath];
    }
    else if ([post.type isEqualToString:@"image"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:CELL_POST_LIST_IMAGE forIndexPath:indexPath];
    }
    [cell setupWithPost:post];

    return cell;
}

Here is setupWithPost method:

- (void)setupWithPost:(Post *)post {
    self.label.text = post.text;
    self.datetimeLabel.text = [Format stringWithDatetime:post.datetime];


    if ([DBHelper didLikePost:post likerID:[Helper currentUserID]]) {
        self.likeButton.enabled = NO;
        self.likeButton.titleLabel.alpha = 0.5;
        self.likeButton.titleLabel.text = [NSString stringWithFormat:@"liked"];
    }
    else {
        self.likeButton.enabled = YES;
        self.likeButton.titleLabel.alpha = 1.0;
        self.likeButton.titleLabel.text = [NSString stringWithFormat:@"like"];
    }

}

the button labels are correct when I first enter the ListViewController(TableViewController), but When I navigate to the next ViewController (DetailViewController), and then navigate back, all button labels become unset (showing whatever text I preset in storyboard)

Interestingly, when I scroll down and then scroll back, all the buttons labels become correct again. I have tried prepareForReuse but not working

NOTE: this applies only to button title labels, normal labels (e.g. datetimeLabel) are always correctly displayed

- (void)prepareForReuse {
    [super prepareForReuse];

    self.label.text = @"";
    self.datetimeLabel.text = @"";
    self.likeButton.titleLabel.text = @"";
}

Answers


that sound very strange, I don't know if this is the cause of the problem but you should set a button title using the API

- (void)setTitle:(NSString *)title forState:(UIControlState)state  

try

[self.likeButton setTitle:@"some title" forState:UIControlStateNormal];

Need Your Help

Objective-C playground?

objective-c cocoa

Is there any sort of Mac app, Web app, or others like JSFiddle for Objective-C/Cocoa purposes?

How to define uniqueidentifier column in SQL Server CE

c# database windows-phone-7

I'm trying to create a local SQL Server CE database in my Windows Phone 7 app.