Jhorra
Jhorra

Reputation: 6321

Cell image in uitableview shows back up when scrolling

I'm doing the following to set the cell image if a message is unread

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];

        Message *m = [self.messages objectAtIndex:indexPath.row];

        cell.textLabel.text = m.subject;
        NSString *detail = [NSString stringWithFormat:@"%@ - %@", m.callbackName, m.dateSent];
        cell.detailTextLabel.text = detail;

        if([m.messageRead isEqualToString:@"False"])
            cell.imageView.image = [UIImage imageNamed:@"new.png"];


    return cell;
}

This is correctly showing an image when it's supposed to. If I scroll down however and scroll back up, they all show the image whether it's supposed to or not

Upvotes: 1

Views: 232

Answers (2)

onegray
onegray

Reputation: 5105

Since UITableViewCells are reused, you should set cell.imageView.image to nil in case you do not need image.

if([m.messageRead isEqualToString:@"False"]) {
    cell.imageView.image = [UIImage imageNamed:@"new.png"];
} else {
    cell.imageView.image = nil;
}

Upvotes: 1

rmaddy
rmaddy

Reputation: 318955

Cells are reused. So you need to set each property for every condition.

if([m.messageRead isEqualToString:@"False"])
    cell.imageView.image = [UIImage imageNamed:@"new.png"];
else
    cell.imageView.image = nil;

Upvotes: 2

Related Questions