UITableViewCell中的按钮

时间:2015-05-04 12:00:35

标签: ios uitableview uibutton didselectrowatindexpath

我有一个带有按钮的UITableViewCell,当我按下按钮时,它移动到下一个视图,但我需要传递TableViewCell的索引,但我找不到这个,因为它不在DidSelectCellAtIndexPath中,它只是可按下的按钮。

如何查找Button的indexPath以及为什么会崩溃?

2 个答案:

答案 0 :(得分:0)

一个非常简单的解决方案: -

(void)btnAction:(id)sender
  CGPoint btnRectPoint = [sender convertPoint:CGPointZero
                                   toView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:btnRectPoint]; // Here you can get your indexpath of table
NSLog(@"index.row=%d",indexPath);

}

答案 1 :(得分:0)

您可以在UIViewController中创建协议并实施委托方法。现在,当您单击UITableViewCell内的按钮时,可以将单元格对象本身作为参数传递给该委托方法。

-(void)protocolMethod:(id)sender {  
    [self.delegate delegateMethod:self];  
}

并在UIViewController中,您可以从表格视图中获取此单元格的索引路径。

-(void)delegateMethod:(MyCustomCell*)cell {
    NSIndexPath *indexPath = [self.objTableView indexPathForCell:cell];
}
相关问题