I read that because of the dequeueReusableCellWithIdentifier method grabbing an oldCell if there is one available to use rather than creating a new one, if I have special views to show, like images or checkmarks, that I want to explicitly say, show = YES or show = NO for the cell so the cell doesn’t end up using a cell with an old graphic on it and I end up getting an image I don’t want on my view.
So in this case, I have a UITableView inside a popover. It is a grouped table. The groups have 2-4 cells depending on the data. I am using the UITableViewCellStyleValue2 like the contacts application. I want to add an image to the right side of the cell on the first cell in the group only depending on the data. It works, however, once I scroll the table, it picks up a dequeued cell and then I’ll get one of the images on the right hand side for a cell that is not even the first cell. I did not create a UITableViewCell subclass, and am unsure if I need to add this point so I have control over the imageView I’m adding to the cell.
So here’s the important part of the cellForRowAtIndexPath method:
if (row == 0) {
UIImage *dot;
if (aTarget.importance == TargetImportanceHigh) {
dot = [UIImage imageNamed:@"dot_red.png"];
}
else if (aTarget.importance == TargetImportanceLow) {
dot = [UIImage imageNamed:@"dot_yellow.png"];
}
else {
dot = [UIImage imageNamed:@"dot_black.png"];
}
UIImageView *imgView = [[UIImageView alloc] initWithImage:dot];
imgView.frame = CGRectMake(cell.frame.size.width - 50, cell.frame.size.height / 2 - 7.5, 15, 15);
[cell.contentView addSubview:imgView];
[imgView release];
cell.textLabel.text = @"Location
cell.detailTextLabel.text = [NSString stringWithFormat:@"%i", aTarget.location];
}
else if (row == 1) {
cell.textLabel.text = @"Coordinate";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", aTarget.coordinate];
}
return cell;
You should do this via a
UITableViewCellsubclass, and then you can clear your secondUIImageViewinprepareForReusee.g.