从 TableViewController 打开新视图

Opening new view from TableViewController

我有 table 包含图像和一些文本的视图。这个想法是打开一个新视图,其中包含特定图像和当用户单击 table 视图中的实例时显示的文本。

我正在考虑通过在 table 视图中的单元格上覆盖一个按钮来简单地解决这个问题,但感觉应该有一个更简单的内置解决方案,比如 onCellClick(Any sender) 函数,但我还没有找到它。请帮忙?

是的,有一个简单的解决方案,那就是使用 UITableViewDelegate 方法didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    UIImage *image = cell.imageView.image;

   // here create a new view controller and send these details to your new view controller.
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //Main = your storyboard name
   DisplayDataViewController *displayDataViewController = [storyboard instantiateViewControllerWithIdentifier:@"displayDataViewController"];
  //set data in new view controller before pushing that on top of your navigation controller
  displayDataViewController.imageName = image;
 displayDataViewController.displayStr = cellText;
   [self.navigationController pushViewController:displayDataViewController animated:NO];
}