UITableViewCell how to change UIImageView when row is touched?
-
17-04-2021 - |
Question
I have UIImageView
in a custom TableViewCell
. I load an image in it with this code:
UIImageView *myImg = (UIImageView *)[cell viewWithTag:kImagePlayer];
myImg.image = [UIImage imageNamed:@"player_play.png"];
So my question is how to change this image when I touch the row? I need to change the image to "music_play".
Thanks!
Solution
In tableView:didSelectRowAtIndexPath:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *myImg = (UIImageView *)[cell viewWithTag:kImagePlayer];
myImg.image = [UIImage imageNamed:@"player_play.png"];
OTHER TIPS
Hi I think this will help
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tempCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *myImg = (UIImageView *)[tempCell viewWithTag:kImagePlayer];
myImg.image = [UIImage imageNamed:@"music_play.png"];
}
BR, Hari
You need to check which row was selected using the tableView:didSelectRowAtIndexPath
method and find the row selected using
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
Then you set the new image to it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow