ios - 从自定义表格单元格中发送参数到 segue?

标签 ios iphone uitableview segue

我有一个 UITableView带有一个自定义表格单元格,上面有几个控件 - 其中一个是按钮。

单击单元格本身时,我通过 segue 将一些数据发送到 View Controller - 效果很好。

当单击单元格内的按钮表单时,我想打开一个不同的 View Controller - 并再次发送一些数据。

该按钮连接到 View Controller ,单击该按钮时该 Controller 可以正常打开。

问题是我无法获得 IndexPath里面prepareForSegue为了设置我希望发送到 View Controller 的参数。

此代码从表数据源中获取第一个对象:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if([segue.identifier isEqualToString:@"CommentSegue"])
 {

    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSDictionary *detail = [myObject objectAtIndex:path.row];
    CommentViewController *navController = segue.destinationViewController;
    navController.data=detail;
 }
}

虽然我想要按钮所属的 n 项...

谢谢。

最佳答案

您永远不应该从单元格本身调用 segue。

您必须为 -tableView: cellForRowAtIndexPath: 中的按钮定义一个目标。填充单元格的数据时。

做这个:
[cell.theButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
要获取该按钮所属行的索引,请使用以下技巧:cell.theButton.tag = indexPath.row
然后,您可以像这样处理对 TableView Controller 的单击:

-(void) buttonClicked:(UIButton *) button
{
     NSInteger index = button.tag;
     // now prepare the data and perform segue
}

关于ios - 从自定义表格单元格中发送参数到 segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643204/

相关文章:

ios - 捕获 UIView 的图像(renderInContext 无法正常工作)

ios - 在 convenience init Swift 3 中获取类名

ios - 使用本地服务器测试在物理设备上运行的XCode应用程序

iphone - 如何将 2 个对象添加到 tableview 单元格中

ios - UITableViewCell子类化,当选择时消失UIImageView

ios - 如何通过编码xcode改变按钮的位置

iphone - 如何从当前 View Controller 的实现文件加载另一个 View Controller ?

iphone - iTunesconnect : renaming app name to app name which has been already removed

iphone - 游戏中的数学逻辑(基本三角函数),这段代码在做什么?

ios - 对 dequeueReusableCellWithIdentifier 使用不同的初始化方法