ios - 点击手势在自定义单元格 View 中不起作用

标签 ios objective-c uitapgesturerecognizer custom-cell

我有一个自定义单元格。我在我的单元格中添加了一个 UILabel 并向其添加了一个点击手势。它应该有效,但对我无效。

这是我的代码:

    UILabel *label = [[UILabel alloc] init];
    label.text = chunk;
    label.userInteractionEnabled = isLink;

    if (isLink)
    {

        // 5. Set tap gesture for this clickable text:
        SEL selectorAction = isTermsOfServiceLink ? @selector(termsPressed) : @selector(privacyPressed);
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                     action:selectorAction];
        [label addGestureRecognizer:tapGesture];

    }

    // 6. Lay out the labels so it forms a complete sentence again:

    // If this word doesn't fit at end of this line, then move it to the next
    // line and make sure any leading spaces are stripped off so it aligns nicely:

    [label sizeToFit];

    if (self.privacyLbl.frame.size.width < wordLocation.x + label.bounds.size.width)
    {
        wordLocation.x = 0.0;                       // move this word all the way to the left...
        wordLocation.y += label.frame.size.height;  // ...on the next line

        // And trim of any leading white space:
        NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*#"
                                                            options:NSRegularExpressionSearch];
        if (startingWhiteSpaceRange.location == 0)
        {
            label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange
                                                             withString:@""];
            [label sizeToFit];
        }
    }

    // Set the location for this label:
    label.frame = CGRectMake(wordLocation.x,
                             wordLocation.y-20,
                             label.frame.size.width,
                             label.frame.size.height);

    label.text  = @"";        
    [self.privacyLbl addSubview:label];

我的方法:

-(IBAction)termsPressed:(id)sender
{
    [self.delegate voorWardenButtonPressed];
}

-(IBAction)privacyPressed:(id)sender
{
    [self.delegate privacyButtonPressed];
}

我尝试过简单的方法而不是IBAction。它仍然没有检测到点击标签。

最佳答案

请确保启用与您的标签关联的 userInteractionEnabled 标志

关于ios - 点击手势在自定义单元格 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28957476/

相关文章:

iphone - 如何为 UILocalnotification 增加/减少 application.badge 号码

ios - 为 SpriteKit 转换 UITapGestureRecognizer 的触摸位置 - Swift

iphone - 允许访问iOS 5.0中我的应用程序的iPhone联系人

ios - 提升中的项目模板

ios - 如何插入数组值以根据长/纬度值绘制多边形-Google Maps iOS?

ios - 在 Objective-C 中从 PDF 中提取可编辑字段

ios - 获取核心文本中点击字符的索引

objective-c - Swift UITapGestureRecognizer

ios - ios 6 和 ios 5.1 之间核心文本的差异?

ios - 如何处理 cookie 以保持用户使用 Swift 登录?