简体   繁体   中英

Tap gesture is not working in custom cell view

I am having a custom cell. I added a UILabel in my cell and added a tap gesture to it. It was supposed to work but it is not working for me.

Here is my code:

    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];

My methods:

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

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

I have tried simple methods rather than IBAction . Still it does not detect tap on labels.

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

Try to replace this

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

on to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM