【问题标题】:UITableView and Selecting ItemsUITableView 和选择项目
【发布时间】:2011-11-19 03:22:44
【问题描述】:

我目前正在创建一个简单的分组 UITableView,当用户选择例如“Apple”时,我希望将我项目中的图像加载到 Apple 的图像视图中。

我在互联网上看到了一些例子,但我想看看还有什么。

任何建议将不胜感激。谢谢。

【问题讨论】:

  • willDisplayCell:forRowAtIndeaPath: 做什么?
  • 它在单元格显示之前被调用。这是你对单元格进行 UI 更改的地方,而不是内容更改。
  • 不清楚你想在这里学习什么。检测表格选择 (-tableView:didSelectRowAtIndexPath:) 并在 UIImageView (-setImage:) 中显示图像是直接操作。你哪里有问题?你看过哪些样本?样品如何让您想寻找其他解决方案?

标签: objective-c ios xcode ios4 xcode4


【解决方案1】:

要么设置cell.imageView.highlightedImage,要么在didSelectRowAtIndexPath方法中,将cell.imageView.image从nil改为一些[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"];
}

【讨论】:

  • 如果我的 UITableView 中有 7 个不同的选项,那会起作用吗?苹果、橙子、香蕉…………
  • 是的,会的。只需使用 indexPath.row 作为数组的 objectForIndex。
猜你喜欢
  • 2011-11-08
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多