I have a tableview that is made up of custom tableview cells which contain an imageview. In cellForRowAtIndexPath I set the image of each cell. I want the image to change when the cell is selected, so in didSelectRowAtIndexPath I get the cell and change the image, no problems there. However, when I scroll the table (read: table reloads the cells) the new image is no longer present. Also when the cell is no longer selected I want the image to switch back to the original.
I have tried doing the following in cellForRowAtIndexPath:
if(cell.isSelected){
cell.imageview.image = [UIImage ImageNamed: @"selected.png"];
}
else
cell.imageview.image = [UIImage ImageNamed: @"not selected.png"];
I have also tried using the BOOL values cell.highlighted or cell.selected to no avail.
Any thoughts are appreciated.
Try using a class variable
selectedCellIndexPathof typeNSIndexPath. IndidSelectRow...you set it’s value, and in thecellForRow...you write:Edit:
Or you could simply write:
but the first solution is a little more efficient.