在不使用属性或将单元格与其表视图耦合的情况下,可以通过触摸其中的控件来获取单元格的索引路径。
要做的第一件事就是目标 - 动作添加到您的按钮,像这样:
[button addTarget:cell action:@selector(buttonTapped:withEvent:) forControlEvents:UIControlEventTouchUpInside];
然后你可以使用事件参数的操作方法:
- (void)buttonTapped:(id)sender withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
// ....
}
或者视图层次结构来检索单元格:
- (void)buttonTapped:(id)sender withEvent:(UIEvent *)event
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// ....
}
或发件人的位置(即控制):
- (void)buttonTapped:(id)sender withEvent:(UIEvent *)event
{
CGPoint position = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:position];
// ....
}
如果我理解正确,你想知道该行是否在屏幕上? –