我如何检测UITableViewCells以外的UITableView的水龙头 – 即在部分标题?

我想要处理不包括单元格的UITableView的水龙头。 换句话说,页眉和页脚。

我可以添加一个手势识别器的标题(没有页脚),但最后一节底部的空间不响应水龙头。

而不是添加上面的手势识别器,我尝试添加一个手势识别器的表,但它阻止tableView:didSelectRowAtIndexPath:被调用。

我尝试了各种各样的UIGestureRecognizerDelegate调用没有太多的运气。

我终于通过在识别器上设置cancelsTouchesInView = NO (我认为这是秘密)并实现了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:在委托中,让tableView被触及并调用tableView:didSelectRowAtIndexPath:

处理程序必须过滤掉单元格上的水龙头,但至less可以工作。 (另外,在UITableView indexPathForRowAtPoint:point发现一个bug indexPathForRowAtPoint:point有时候会返回无效的indexPaths 。)

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

如果您不需要了解 – 点击了什么(页脚或页眉),您可以尝试以下方法。

viewDidLoad方法中添加手势识别器。

 - (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p]; if (indexPath == nil) NSLog(@"long press on table view but not on a row"); else { NSLog(@"long press on table view on a row"); } } 

注意! 我没有testing过这个代码:)

这是我一起去的代码。

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; } 

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]; } } } 

你有没有尝试在viewForHeaderInSection中添加longGesture?

如果不是这样的话:

 -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel *headerLabel = [[UILabel alloc]init]; headerLabel.tag = section; headerLabel.userInteractionEnabled = YES; headerLabel.backgroundColor = [UIColor greenColor]; headerLabel.text = [NSString stringWithFormat:@"Header.%d",section]; headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height); UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hederClicked:)]; tapGesture.cancelsTouchesInView = NO; [headerLabel addGestureRecognizer:tapGesture]; return headerLabel; //return nil; } -(void)hederClicked:(UIGestureRecognizer*)sender { UILabel *lbl = (UILabel*)sender.view; NSLog(@"header no : %d", lbl.tag); } 

将手势识别器添加到您自己的部分页眉和页脚视图是正确的。

要反过来触摸最后一行下方的区域,请在表格中添加页脚视图。 将手势识别器添加到此表格页脚。 使该页脚视图与屏幕一样高。

唯一的缺点是,如果行不填满屏幕,则会丢失最后一行以下的行。 如果您真的想要它们,请设置自定义表格页脚视图以绘制线条来模仿行。

在TableView中添加标题视图,如下所示:

表格中标题视图的高度

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 100; } 

同时在标题中添加视图并在此视图中进行挑选。

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)]; /* Create custom view to display section header... */ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 100)]; [label setFont:[UIFont boldSystemFontOfSize:12]]; /* Section header is in 0th index... */ [label setText:@"Header View"]; [viewHeader addSubview:label]; [viewHeader setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; // Add TapGestureRecognizer UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; [recognizer setNumberOfTapsRequired:1]; recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view [viewHeader addGestureRecognizer:recognizer]; return viewHeader; } 

TapGestureRecognizer mehode

 - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { [[[UIAlertView alloc]initWithTitle:@"Demo" message:@"Test On header Click" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; } } 

你的输出:

在这里输入图像说明

这是最简单最有效的实现:

使用这些UITableViewDelegate方法(iOS6 +):

 -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger) - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section 

您可以使用这些方法轻松地将UITapGestureRecognizer添加到页脚/页眉。

例如:

  - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)]; [view addGestureRecognizer:recognizer]; } - (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer { NSLog(@"Tapped"); }