添加手势识别器到tableView标头不起作用? [英] Add a gesture recognizer to tableView header doesn't work?

查看:99
本文介绍了添加手势识别器到tableView标头不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道表格视图标题(表格视图的最顶部)是一个View
所以我尝试添加一个UITapGestureRecognizer,但它不起作用......

I know that a "table view header"(the most top-part of a table view) is a View So I try to add a UITapGestureRecognizer to it ,but it doesn't work...

代码很简单:

- (void)tap:(UITapGestureRecognizer *)recognizer
{
    // do something
}

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]    initWithTarget:self action:@selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];

这里有什么提示要小心吗?非常感谢

Any tips here to care ? thanks a lot

推荐答案

这对我有用:
相反添加:

Here's the thing that works for me: Instead adding this:

self.tableView.tableHeaderView



<我在tableview上的每个UILabel上都添加了手势识别器。

I add gesture recognizer on every UILabel on tableview.

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

我希望有所帮助。

这篇关于添加手势识别器到tableView标头不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆