【问题标题】:UITableView didSelectRowAtIndexPath [duplicate]UITableView didSelectRowAtIndexPath [重复]
【发布时间】:2014-12-22 12:34:52
【问题描述】:

我有一个包含五个单元格的 UITableView。每个单元格都有一个标签和一个图像。现在使用 didSelectRowAtIndexPath,当我单击任何单元格时,应该会出现一个新视图,其中包含来自单击的单元格的标签和图像。代码是:

OpenMessageViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"OpenMessageVC"];
    [self.navigationController pushViewController:controller animated:YES];

OpenMessageViewController 是我在单击单元格时出现的新视图。 我在情节提要的 OpenMessageViewController 中添加了标签和图像。如何将 OpenMessageViewController 的标签文本和图像更改为单击的单元格中存在的文本和图像。 谢谢你

【问题讨论】:

  • 您的实际问题是什么?您的代码没有任何问题,除非您的 Storyboard 实际上不包含可见的 UINavigationController。
  • 我已经更新了我的问题
  • 最好搜索如何将对象从一个 ViewController 传递到另一个。因为在 View 中导航时需要跟踪 TableView Cell 的索引。
  • 看看这个不错的教程codigator.com/tutorials/…

标签: ios uitableview


【解决方案1】:

我还没有把你的代码放在 Xcode 项目中,但是你不能做这样的事情来传递图像和文本吗?

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the tapped cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// Configure the destination VC
UIImageView *myImageView = (UIImageView *)[cell viewWithTag:10]; // Set tag 10 to the UIImageView in your cell
UILabel *myLabel = (UILabel *)[cell viewWithTag:20]; // Set tag 20 to the UILabel in your cell
controller.imageView.image = myImageView.image;
controller.theLabel.text = myLabel.text;

// Navigate
[self performSegueWithIdentifier:@"mySegue" sender:self];
}

记得在 IB 中连接两个 ViewController。 然后你必须有一个名为 theLabel 的标签和一个名为 imageView 的目标 ViewController 上的连接到插座的图像视图。 (请随意更改名称)

【讨论】:

  • 您可能需要进行一些调整以使我的解决方案适合您的“控制器”参考。如果您有任何问题,请告诉我
  • 成功了..:)谢谢你
  • @mahivamsi 很高兴为您提供帮助!;)
猜你喜欢
  • 2011-11-21
  • 2012-01-14
  • 2014-09-06
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2012-02-18
相关资源
最近更新 更多