假设我有一个自定义 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/