如何检测UITableViewCells之外的UITableView上的点击 - 即在标题部分?

Mat*_*att 6 uitableview uigesturerecognizer ios

我想处理UITableView不包括细胞的水龙头.换句话说,页眉和页脚.

我可以为标题添加手势识别器(没有页脚),但最后一部分底部的空格不响应点击.

我没有添加上面的手势识别器,而是尝试在表格中添加手势识别器,但它阻止了tableView:didSelectRowAtIndexPath:被调用.

UIGestureRecognizerDelegate没有太多运气,尝试了各种各样的电话.

我终于通过设置cancelsTouchesInView = NO识别器(我认为这是秘密)并gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:在委托中实现以便tableView接触并调用它来工作tableView:didSelectRowAtIndexPath:

处理程序必须过滤单元格上的点击,但至少它可以工作.(另外,发现了一个有时UITableView indexPathForRowAtPoint:point返回无效的bug indexPaths.)

我的问题是:是否有更好的方法来获取单元格视图以防止触摸/手势进入表格?(如果没有人给我任何更好的想法,我会发布代码.)

Mat*_*att 4

这是我使用的代码。

1 将点击手势识别器添加到表格中。设置取消视图中的触摸未选中。

2 允许手势穿过桌子。

#pragma mark - UIGestureRecognizerDelegate

/**
 * Prevents the tap gesture recognizer in the table from gobbling taps
 * and allows the table to perform tableView:didSelectRowAtIndexPath:
 * <p>
 * The gesture recognizer must have cancelsTouchesInView == NO to allow
 * the touches through to the table.
 * <p>
 * The action must check that the tap occurred outside of cells.
 *
 * @see handleTap:
 */
- (BOOL) gestureRecognizer:(UIGestureRecognizer*)recognizer
  shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)o {
    return recognizer.view == self.table;
}
Run Code Online (Sandbox Code Playgroud)

3 检查水龙头不在单元格中。

/**
 * Filters a tap on the table to those on the headers/footer.
 * <p>
 * Note, indexPathForRowAtPoint: has a bug and returns valid indexPaths
 * for taps in the last section header and table footer. So an extra
 * check is made to ensure that the tap was, in fact, in the cell.
 */
- (IBAction) handleTap:(UITapGestureRecognizer*)tap {
    if (UIGestureRecognizerStateEnded == tap.state) {
        CGPoint point = [tap locationInView:tap.view];
        NSIndexPath* index = [self.table indexPathForRowAtPoint:point];
        UITableViewCell* cell;

        if (index) {
            cell = [self.table cellForRowAtIndexPath:index];
            point = [tap locationInView:cell];

            if (point.y < 0 || point.y >= cell.frame.size.height) {
                index = nil;
            }
        }

        if (!index) {
            [self.view performSelector:@selector(endEditing:)
                            withObject:@(YES) afterDelay:0];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)