点击手势在自定义单元格视图中不起作用

时间:2015-03-10 06:32:28

标签: 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。仍然没有检测到标签上的点击。

2 个答案:

答案 0 :(得分:1)

请务必启用与您的标签相关联的userInteractionEnabled标记

答案 1 :(得分:0)

尝试替换此

SEL selectorAction = isTermsOfServiceLink ? @selector(termsPressed) : @selector(privacyPressed);

对此:

SEL selectorAction = isTermsOfServiceLink ? @selector(termsPressed:) : @selector(privacyPressed:);