3

我刚刚添加了一个UITapGestureRecognizer到我的UITableView,但处理程序(下)只是给 'null' 为indexPath.

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

我不明白为什么。

4

1 回答 1

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
}
于 2012-08-22T10:40:15.387 回答