我知道“表视图标题”(表格视图的最顶部)是一个视图 所以我尝试添加一个UITapGestureRecognizer,但它不起作用......
代码很简单:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// do something
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
这里有什么提示要关心吗?非常感谢
答案 0 :(得分:4)
这对我有用: 而是添加这个:
self.tableView.tableHeaderView
我在桌面视图上的每个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);
}
我希望有所帮助。
答案 1 :(得分:3)
首先 确保在viewDidLoad或viewWillAppear
中调用此代码部分UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
其次,请确保
self.tableView.tableHeaderView
不为null,添加
NSLog([self.tableView.tableHeaderView description]);
检查控制台输出
我刚刚尝试了您的代码,并且正确收到了水龙头