可能重複:
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