CGPoint的indexPath

时间:2012-08-22 10:30:58

标签: ios uitableview uigesturerecognizer

我刚刚向UITapGestureRecognizer添加了UITableView,但处理程序(下方)只为indexPath提供了'null'。

- (void)photoTapped:(UIGestureRecognizer *)gestureRecognizer {
            if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
                CGPoint tapLocation = [gestureRecognizer locationInView:self.itemsTableView];
                NSIndexPath *tappedIndexPath = [self.itemsTableView indexPathForRowAtPoint:tapLocation];
               //...
        }
}

我不明白为什么。

1 个答案:

答案 0 :(得分:3)

这样做:

 - (void)photoTapped:(UIGestureRecognizer *)gestureRecognizer {
 CGPoint location = [gestureRecognizer locationInView:self.view];

if (CGRectContainsPoint([self.view convertRect:self.itemsTableView.frame fromView:self.itemsTableView.superview], location))
{
    CGPoint locationInTableview = [self.itemsTableView convertPoint:location fromView:self.view];
    NSIndexPath *indexPath = [self.itemsTableView indexPathForRowAtPoint:locationInTableview];
    if (indexPath) //use this code if needed
        [self tableView:self.itemsTableView didSelectRowAtIndexPath:indexPath];

    return;
}

// otherwise proceed with the rest of your gesture handling logic
}