试试这个:
- (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"]];
}
或者...(替代方法)
由于您的单元格是UITableViewCellStyleDefault 样式,您甚至不需要创建自己的UIImageView 对象,因为UITableViewCellStyleDefault 已经提供了一个预定义 imageView 对象(只是比如textLabel & detailTextLabel)。
这个imageView 对象出现在单元格的左侧。
简单地说:
cell.imageView.image = [UIImage imageNamed:@"image.png"];
例子:
- (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"]];
}
至于仅在一个单元格中显示图像(取决于最后选择哪个),我已经回答了here(您的其他问题):How to set image in selected cell row in UITableView
编辑:事实上,我已经合并了我的答案,下面继续。
终于...
上述方法的问题在于它只是一个视觉方面。
如果您选择第一行并向下滚动并稍后返回第一行,则图像将不再存在,因为-cellForRowAtIndexPath: 已被调用,并且由于其中有[cell.imageView setImage: [UIImage imageNamed:@""]];,imageView.image 被重置。
要处理上述情况,您需要记住选择了哪些行。 (你可以通过一个基本的数组来实现)
示例:
- (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];
}