我知道“ TableView 标题”( TableView 的最顶部)是一个 View 所以我尝试向它添加一个 UITapGestureRecognizer,但它不起作用...
代码很简单:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// do something
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
这里有什么需要注意的提示吗?非常感谢
最佳答案
这是对我有用的东西: 而是添加:
self.tableView.tableHeaderView
我在 tableview 上的每个 UILabel 上添加了手势识别器。
-(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 No.%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(catchHeaderGesture:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
//return nil;
}
-(void)catchHeaderGesture:(UIGestureRecognizer*)sender
{
UILabel *caughtLabel = (UILabel*)sender.view;
NSLog(@"header no : %d", caughtLabel.tag);
}
希望对您有所帮助。
关于ios - 向 tableView header 添加手势识别器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861669/