ios - 获取 UITableViewCell 内的按钮单击

我有一个带有表格 View 的 View Controller 和一个用于表格单元格模板的单独 Nib 。单元格模板有一些按钮。我想访问按钮单击以及在我定义了表格 View 的 View Controller 内单击的单元格的索引。

所以我有 ViewController.hViewController.m 我有 UITableViewTableTemplate.h , TableTemplate.mTableTemplate.xib 我定义了 Nib 。我想要 ViewController.m 中带有单元格索引的按钮单击事件。

有什么帮助吗?

最佳答案

1) 在您的 cellForRowAtIndexPath: 方法中,将按钮标签指定为索引:

cell.yourbutton.tag = indexPath.row;

2) 为您的按钮添加目标和操作,如下所示:

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) ViewControler中基于索引的代码操作如下:

-(void)yourButtonClicked:(UIButton*)sender
{
     if (sender.tag == 0) 
     {
         // Your code here
     }
}

多个部分的更新:

您可以查看this link在表格 View 中检测多行和多节的按钮单击。

https://stackoverflow.com/questions/20655060/

相关文章:

ios - 如何在 View 出现之前滚动到 iPhone 上 UITableView 的底部

ios - 不推荐使用旋转方法,相当于 'didRotateFromInterfaceOrienta

ios - 如何以简单的方式将 CGPoint 对象添加到 NSArray?

ios - 我可以更改 NSLayoutConstraint 的乘数属性吗?

ios - UITableViewCell 分隔符在 iOS7 中消失

iphone - 如何使用可选方法创建协议(protocol)?

objective-c - xcode4中框架和静态库的区别,以及如何调用它们

ios - 如何调整 UIButton 的 imageSize?

ios - 模仿Facebook隐藏/显示扩展/收缩导航栏

iphone - 如何处理包含属性的 Objective-C 协议(protocol)?