如何在tableView中使用Seque查找indexPathbutton

我有一个使用原型单元的UITableView

单元格有一个使用Segue显示UIViewController (Modal)的UIButton

我的问题是:如何将IndexPath的单元格传递给第二个视图控制器

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showGamerDetail"]) { //its not worked: NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow]; nDetailViewController *destinationViewController = segue.destinationViewController; destinationViewController.selectedRowTitle = [gamers objectAtIndex:indexPath.row]; destinationViewController.indexPath = indexPath; destinationViewController.delegate = self; } 

暂时忘记你正在使用故事板,让我们试着按照最好的方法。

你有几个解决scheme,但在我看来,只有一个是最优的: delegation pattern

首先,你应该扩展你的单元格 ,并使用委托来返回单元格,当用户按下button。 然后,你应该使用indexPathForCell来获取indexPath

让我们看看方法:

单元格button位置

 - (void)buttonClicked:(id)sender CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *tappedIP = [self.tableView indexPathForRowAtPoint:buttonPosition]; // When necessary // UITableViewCell *clickedCell = [self.tableView cellForRowAtIndexPath:tappedIP]; } 

上面的解决scheme当然是最快速的实现 ,但从devise/架构的angular度来看并不是最好的 。 此外,你得到的indexPath但你需要然后计算其他信息。 这是一个很酷的方法,但是会说不是最好的。

单元格由button超级循环

 // ContactListViewController.m - (IBAction)emailContact:(id)sender { YMContact *contact = [self contactFromContactButton:sender]; // present composer with `contact`... } - (YMContact *)contactFromContactButton:(UIView *)contactButton { UIView *aSuperview = [contactButton superview]; while (![aSuperview isKindOfClass:[UITableViewCell class]]) { aSuperview = [aSuperview superview]; } YMContactCell *cell = (id) aSuperview; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; return [[self fetchedResultsController] objectAtIndexPath:indexPath]; } 

以这种方式获得细胞是前者的性能较差,也不是优雅的。

单元格button标记

 - (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.theLabel.text = self.theData[indexPath.row]; cell.button.tag = indexPath.row; [cell.button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside]; return cell; } -(void)doSomething:(UIButton *) sender { NSLog(@"%@",self.theData[sender.tag]); //sender.tag will be equal to indexPath.row } 

绝对没有。 使用标签似乎是一个很酷的解决scheme,但控件标签可以用于其他原因 ,如下一个响应者等。我不喜欢这种方法。

细胞按devise模式

 // YMContactCell.h @protocol YMContactCellDelegate - (void)contactCellEmailWasTapped:(YMContactCell*)cell; @end @interface YMContactCell @property (weak, nonatomic) id<YMContactCellDelegate> delegate; @end // YMContactCell.m - (IBAction)emailContact:(id)sender { [self.delegate contactCellEmailWasTapped:self]; } // ContactListViewController.m - (void)contactCellEmailWasTapped:(YMContactCell*)cell; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; YMContact *contact = [[self fetchedResultsController] objectAtIndexPath:indexPath]; // present composer with `contact` ... } 

这是我最喜欢的解决scheme 。 使用delegationblocks是一个非常好的方法, 你可以传递你想要的所有参数 。 事实上,你可能想直接发回所需的信息,而不必稍后计算。

请享用 ;)

非常简单的解决scheme,如果你想指向选定单元格的相同segue。

 - (IBAction)buttonPressed:(id)sender { CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; } 

首先做一个全局的intvariablesselecteIndex ;

在你的单元格中,给你的button标签类似于单元格的indexPath.row

例如btn.tag = indexPath.row;

点击该button的抠听和呼叫操作

 [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; -(void)buttonClicked:(id)sender { UIButton *btn = (UIButton*)sender; selectedIndex = btn.tag; [self performSegueWithIdentifier:@"segueName" sender:self]; } 

然后在你的观点结束写这个function

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"segueName"]) { viewController *dvc = (viewController *)[segue destinationViewController]; [dvc setSelected:selectedIndex]; }; }