CGPoint的indexPath

3
我刚刚给我的UITableView添加了一个UITapGestureRecognizer,但是下面的处理程序返回indexPath为“null”。

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

我不明白为什么。
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
}

尝试了一下 UICollectionView,完美地运行了! - Sheharyar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接