Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
//Create UIImageView object locally (so each cell has separate UIImageView)
UIImageView *cellSpecificImage = [[UIImageView alloc] initWithFrame:CGRectMake(7,10, 20, 25)];
[cellSpecificImage setImage:[UIImage imageNamed:@""]];
[cellSpecificImage setTag:100]; //important
//add the UIImageView object to the contentView of the cell
//[cell addSubView:cellSpecificImage]; //incorrect
[cell.contentView adSubView:cellSpecificImage]; //correct
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//refresh all cells
//basically call cellForRowAtIndexPath to reset all cells
[tableView reloadSections:indexPath.section
withRowAnimation:UITableViewRowAnimationFade];
//get cell for the currently selected row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//basically, pointer to the cell's UIImageView object
UIImageView *tmpCellSpecificImage = (UIImageView *)[cell viewWithTag:100];
//change parameters and it will manifest on the cell
[tmpCellSpecificImage setImage:[UIImage imageNamed:@"loudspeaker.png"]];
}
Or... (Alternative Method)
Since your cell is of style UITableViewCellStyleDefault
, you need not even create your own UIImageView
object because a predefined imageView
object is already provided by UITableViewCellStyleDefault
(just like textLabel
& detailTextLabel
).
This imageView
object appears on the left side of the cell.
Simply:
cell.imageView.image = [UIImage imageNamed:@"image.png"];
Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
[cell.imageView setImage: [UIImage imageNamed:@""]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//refresh all cells
//basically call cellForRowAtIndexPath to reset all cells
[tableView reloadSections:indexPath.section
withRowAnimation:UITableViewRowAnimationFade];
//get cell for the currently selected row
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//change image
[cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
}
As for having the image displayed in only one cell (depending on which was selected last), I have answered that here (your other question):
How to set image in selected cell row in UITableView
EDIT: infact, i've merged my answers, continued below.
Finally...
The problem with the above method is that it's just a visual aspect.
If you select the first row and scroll down and later return to the first row, the image would no longer be there because -cellForRowAtIndexPath:
is recalled and since we had [cell.imageView setImage: [UIImage imageNamed:@""]];
in it, the imageView.image
is reset.
To handle the above scenario, you need to remember which rows were selected. (you can achieve this by a basic array)
Example:
- (void)viewDidLoad {
[super viewDidLoad];
//arrMyDataSource is defined in .h as 'NSArray *arrMyDataSource;'
//needless to say but this is the tableView's datasource contents
arrMyDataSource = @[@"Apple",@"Banana",@"Candy",@"Door",@"Elephant"];
//arrMemoryMap is defined in .h as 'NSMutableArray *arrMemoryMap;'
//this is one way to remember which row is selected
arrMemoryMap = [NSMutableArray alloc] init];
for (int i = 0 ; i < arrMyDataSource.count ; i++) {
NSNumber *tmpSelectStatus = [NSNumber numberWithBool:NO];
[arrMemoryMap addObject: tmpSelectStatus];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
if([[arrMemoryMap objectAtIndex:indexPath.row] boolValue] == YES) {
[cell.imageView setImage: [UIImage imageNamed:@"loudspeaker.png"]];
} else {
[cell.imageView setImage: [UIImage imageNamed:@""]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//first, reset arrMemoryMap for all rows
for (int i = 0 ; i < arrMemoryMap.count ; i++) {
if([[arrMemoryMap objectAtIndex:i] boolValue] == YES]) {
[[arrMemoryMap objectAtIndex:i] setBoolValue: NO];
break;
}
}
//now, set current row as selected in arrMemoryMap
[[arrMemoryMap objectAtIndex:indexPath.row] setBoolValue: YES];
//reload data or particular section
[tableView reloadSections:indexPath.section
withRowAnimation:UITableViewRowAnimationFade];
}