我有一个 UITableView
,里面有几个 UITableViewCells
。因为我只有几个单元格,所以表格 View 中有一个区域没有被单元格覆盖。我想在空白区域被点击时做一些事情。
我尝试在 TableView 上添加 UITapGestureRecognizer
。它检测到空白区域的敲击,但随后细胞无法响应敲击。我尝试在表格 View 的 super View 上添加点击手势识别器,但结果是一样的。
一定有办法做到这一点,但我还不太明白。有什么办法可以实现我想做的事情吗?
最佳答案
试试这个: 初始化 UITapGestureRecognizer 并将其添加到您的 tableView:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
gr.delaysTouchesBegan = YES;
gr.delegate = self;
[_tableView addGestureRecognizer:gr];
实现手势识别委托(delegate)方法:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
CGPoint tapPoint = [gestureRecognizer locationInView:_tableView];
UIView * clickedView = [_tableView hitTest:tapPoint withEvent:nil];
NSString *viewClassName = NSStringFromClass(clickedView.class);
return ![viewClassName hasPrefix:@"UITableViewCell"];
}
这样,您在单元格外(但在表格 View 内)的每次点击都会被您的 UITapGestureRecognizer
识别
关于ios - 如何处理 UITableView 非单元格区域的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23847801/