ios - 如何检测准确选择了哪个单元格的按钮

标签 ios xcode uitableview interface-builder

假设我有一个自定义 UITableViewCell 包含 UIButton 控件。当用户按下按钮时,它应该根据选择的单元格向另一个 View Controller 显示适当的信息(更准确地说,我有一个对象数组用于表示 UITableView 中的信息,我想要将此信息传输到下一个 View Controller )。问题是我怎样才能检测到哪个单元格上的按钮被准确选中?

提前致谢。

最佳答案

有很多方法可以做到这一点..但是 superView 的方式从 iOS8 开始就不再可用了。我正在分享一个在所有 iOS 中都能完美运行的代码。

在你的 cellForRow 方法上写这个选择器

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];

现在写这个方法

-(void)btnAction: (UIButton *)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblView];
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%li",(long)indexPath.row);
}

关于ios - 如何检测准确选择了哪个单元格的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290516/

相关文章:

iphone - 构建和测试多个应用程序 - 尝试了解 appID 和配置文件

iphone - 清理 iPhone 模拟器

ios - 如何在 didSelectRowAtIndexPath 中触发选定 Nib 单元格的某些转场

ios - 如何重叠 UITableViewCells?

ios - TestFlight:尝试下载时出现消息 “Unable to Download Application”

ios - 如何以非侵入方式检测 UIControl 之外任何地方的交互?

ios - 在 iOS8 上使用 UIImagePicker 结果 URL 和 PHAsset 从 "My Photo Stream"加载图像

iphone - 胎面 9 : EXC_BAD_ACCESS

ios - XCode 6.3 在归档应用程序时崩溃

ios - 在不处于编辑模式时对 tableView 中的行重新排序