ios - 向 tableView header 添加手势识别器不起作用?

标签 ios ios5 uigesturerecognizer

我知道“ 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/

相关文章:

ios - CocoaPods 未与项目链接

ios - Xcode Utilities rigth 面板在 Storyboard 中不再主宰信息

IOS:日期的字符串拆分

iphone - 如何在 cocos2d 中获得跟随 Sprite 的粒子效果?

html - iOS5 (Safari) 的一个错误,一个 "position:fixed' div 和高大的表格

iphone - 获取 ViewController 的名称

iOS:如何从 Swift 类触发 Obj-C 中的 UITapGestureRecognizer 和 UIPanGestureRecognizer?

ios - 我如何向量化这个 for 循环?

iphone - 我的 iphone 应用程序因这个原因被拒绝 "We found that your app does not follow the iOS Data Storage Guidelines,..."

ios - 获取应用的所有触摸事件