我有一个自定义单元格。我在我的单元格中添加了一个 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/