我有一个 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/