Reputation: 6051
I am trying to import an image in just cell number 1 and 2 ! , but I the result is my image will show in last cell ! I do not know why !! this is the picture that shows my situation :
// Configure the cell.
NSUInteger row = [indexPath row];
cell.textLabel.text = [titles objectAtIndex:row];
cell.detailTextLabel.text = [subtitle objectAtIndex:row];
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
}
return cell;
}
Upvotes: 1
Views: 2221
Reputation: 3637
You might have used "static NSString *reuseIdentifier = @"Identifier" ", this means that all cells will be considered same by this reuseIdentifier. Only visible cells would be different so for example, if there are 5 visible cells on device, then only 5 new cells would be allocated for cell, and then if you scroll down or up, these 5 cells will be reused if you specified reuseIdentifier statically.
I would suggest to make cell uniquely identified by reuseIdentifier, change the above line to "NSString *reuseIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row]" This would solve the issue.
Hope this helps.
Upvotes: 2
Reputation: 81
try this it might help you
case 0:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
default
cell.imageView.image=nil;
Upvotes: 1
Reputation: 2177
The first thing that it makes me think of is that the first cell gets somehow recycled and used for the last cell. You can try to set the image view to nil for every cell and set it just in the first two cells. Should be something like this:
cell.imageView.image = nil;
Hope it helps! ;D
Upvotes: 2
Reputation: 3051
Your cells are reused by the tableview to save memory. You have to tell the cell not to use an image on every other cell. Modify the switch
command like this:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}
Sandro Meier
Upvotes: 1
Reputation: 58478
Either in your tableView:cellForRowAtIndexPath:
set the imageView
's image to nil before conditionally checking to set the new image or implement prepareForReuse
on your cell subclass and set all of the cell's views values to nil.
This will ensure that reused cells are 'clean' before they're brought on screen.
Alternatively you could edit your switch
to look like:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:@"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}
Upvotes: 4