gpt4 book ai didi

ios - 试图在 UITableView 内向 UILabel 添加点击手势 - 未调用点击手势

转载 作者:行者123 更新时间:2023-12-01 19:00:28 27 4
gpt4 key购买 nike

我已经看到关于该主题的其他示例/问题,但我无法让我的工作。

我有一个自定义表格 View 单元格,我在其中添加了一个标签。用户将能够触摸标签以改变其颜色。但是,当我点击它调用的标签时 didSelectCellAtIndexPath并且不注册水龙头。

我希望标签获得优先权,因此如果按下标签是更改颜色而不是调用 didSelectCellAtIndexPath .

但是如果我选择标签之外的一个区域,那么它就像一个正常的单元格并调用 didSelectCellAtIndexPath .

这是我的自定义单元格:

@implementation CustomTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

self.backgroundLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320,150)];
self.overLayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320,150)];
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160,150)];

[self insertSubview:self.backgroundLabel atIndex:0];
[self insertSubview:self.overLayLabel atIndex:1];
[self insertSubview:self.nameLabel atIndex:2];


self.selectFavColour = [[UILabel alloc] initWithFrame:CGRectMake(270, 0, 60, 60)];
self.selectFavColour.backgroundColor=[UIColor whiteColor];
self.selectFavColour.userInteractionEnabled=YES;

[self insertSubview:self.selectFavColour atIndex:3];

self.selectionStyle = UITableViewCellSelectionStyleNone;


UITapGestureRecognizer * tapPress = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
tapPress.numberOfTapsRequired=1;
tapPress.delegate=self;
tapPress.delaysTouchesBegan=YES;
tapPress.cancelsTouchesInView=YES;
[self.selectFavColour addGestureRecognizer:tapPress];
}
return self;
}



-(void)tapPress: (UIGestureRecognizer*) tap{

NSLog(@"Tap press");

}

最佳答案

尝试将其添加到 contentView , 对于您添加到表格单元格的任何 View ,请将其添加到单元格的 contentView
例如

    [self.contentView insertSubview:self.backgroundLabel atIndex:0];
[self.contentView insertSubview:self.overLayLabel atIndex:1];
[self.contentView insertSubview:self.nameLabel atIndex:2];
[self.contentView insertSubview:self.selectFavColour atIndex:3];

希望这对你有帮助

关于ios - 试图在 UITableView 内向 UILabel 添加点击手势 - 未调用点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com