可能重复:
UITableView and Selecting ItemsUITableView - didSelectRowAtIndexPath问题?
我建立一个iPad应用程序,并在一个UITableView十个项目。当我选择“Apple”选项时,我希望它在右侧的UIImageView中显示一个苹果。当我选择“橙色”时,我希望它在右侧的UIImageView中显示橙色,等等。
什么是最简单的代码来完成这个?
可能重复:
UITableView and Selecting ItemsUITableView - didSelectRowAtIndexPath问题?
我建立一个iPad应用程序,并在一个UITableView十个项目。当我选择“Apple”选项时,我希望它在右侧的UIImageView中显示一个苹果。当我选择“橙色”时,我希望它在右侧的UIImageView中显示橙色,等等。
什么是最简单的代码来完成这个?
假设你已经保存你的价值观,你是在某种NSArray的或其他类型的数据源(我要去承担的NSArray)的显示,那么你可以做这样的事情:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selection = [myArray objectAtIndex:indexPath.row];
if ([selection isEqualToString:@"Apple"])
{
[myImageView setImage:[UIImage imageNamed:@"Apple.png"]];
}
elseif ([selection isEqualToString:@"Orange"])
{
[myImageView setImage:[UIImage imageNamed:@"Orange.png"]];
}
}
我为你回答了这个问题。
这将是一个好主意,如果你仍然需要一个UITableView帮助到同样的问题作出回应与评论。
这是从另一个问题的答案。
要么设置cell.imageView.highlightedImage,或在didSelectRowAtIndexPath方法方法中,从cell.imageView.image零到某些改变[UIImage的imageNamed:@ “myImage.png”]:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCustomCellID = @"com.me.foo.cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
cell.imageView.image = nil;
cell.imageView.highlightedImage = [UIImage imageNamed:@"myImage.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(iPadRootViewControllerTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do this if you didnt set the highlightedImage in cellForRowAtIndexPath
// [self tableView:tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:@"myApple.png"];
}
编辑(添加其他问题的意见回答):
从一个行的一个数组得到一个特定的项目,只需使用indexPath.row作为objectForIndex您的阵列。
大厦关闭的Thuggish Nugget's answer,你可以纳入选择名称权成图像串:
NSString *selection = [myArray objectAtIndex:indexPath.row];
[myImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", selection]]];
而且你可以凝结下来成一行。
非常感谢。你是超级有用的! – Brandon