问题描述
限时送ChatGPT账号..我想在不包括单元格的 UITableView
中处理点击.换句话说,就是页眉和页脚.
I want to handle a tap in a UITableView
not including the cells. In other words, the headers and footers.
我可以向页眉添加手势识别器(没有页脚),但最后一节底部的空间不会响应点击.
I can add a gesture recognizer to the headers (there are no footers), but the space at the bottom of the last section does not respond to a tap.
我没有添加上面的手势识别器,而是尝试向表中添加手势识别器,但它阻止了 tableView:didSelectRowAtIndexPath:
被调用.
Instead of adding the gesture recognizers above, I tried adding a gesture recognizer to the table, but it prevents tableView:didSelectRowAtIndexPath:
being called.
我尝试了各种 UIGestureRecognizerDelegate
调用,但运气不佳.
I tried all sorts of UIGestureRecognizerDelegate
calls with not much luck.
我终于通过在识别器上设置 cancelsTouchesInView = NO
(我认为这是秘密)并在委托中实现 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
来实现这一点,以便tableView
接触并调用 tableView:didSelectRowAtIndexPath:
I finally got that to work by setting cancelsTouchesInView = NO
on the recognizer (I think this was the secret) and implementing gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
in the delegate so that the tableView
got touches and called tableView:didSelectRowAtIndexPath:
处理程序必须过滤掉单元格上的点击,但至少它可以工作.(另外,在 UITableView
indexPathForRowAtPoint:point
中发现了一个错误,有时会返回无效的 indexPaths
.)
The handler has to filter out taps on the cells, but at least it works. (Also, found a bug in UITableView
indexPathForRowAtPoint:point
which returns invalid indexPaths
sometimes.)
我的问题是:是否有更好的方法来获取单元格视图以防止触摸/手势进入表格?(如果没有人给我更好的想法,我会发布代码.)
My question is: Is there a better way of getting the cell views to prevent touches/gestures getting through to the table? (I will post code if nobody gives me any better ideas.)
推荐答案
这是我使用的代码.
1 向表格添加点击手势识别器.取消选中取消视图中的触摸.
1 Add a tap gesture recognizer to the table. Set Cancels touches in view unchecked.
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;
}
3 检查水龙头不在单元格中.
3 Check the tap is not in a cell.
/**
* 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];
}
}
}
这篇关于如何检测 UITableViewCells 之外的 UITableView 上的点击 - 即在部分标题中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论