【问题标题】:Trying to add tap gesture to UILabel inside of UITableView - tap gesture not being called尝试在 UITableView 内向 UILabel 添加点击手势 - 未调用点击手势
【发布时间】:2014-06-09 14:14:32
【问题描述】:

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

我有一个自定义表格视图单元格,我已向其中添加了一个标签。用户将能够触摸标签以改变其颜色。但是,当我点击标签时,它会调用 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");

}

【问题讨论】:

  • wy 你在索引 3 处插入 [self insertSubview:self.selectFavColour atIndex:3]; 只需将其添加到内容视图 [self.contentView addsubview:self.selectFavColour]; 并检查天气方法是否正在调用,并确保在 @987654328 之上没有其他视图@
  • 对不起,我忽略了我在索引 1 处有图像,在索引 2 处有图像,这就是它在索引 3 处的原因。我会更新问题
  • @Shan 请查看更新后的问题
  • 你是否使用self.backgroundLabel self.overLayLabel 具有相同的框架,如果可能的话,还发布屏幕截图你想如何在单元格中显示标签

标签: ios uitableview uitapgesturerecognizer didselectrowatindexpath


【解决方案1】:

尝试将其添加到contentView,对于您要添加到表格单元格的任何视图,请将其添加到单元格的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];


希望对你有帮助

【讨论】:

    猜你喜欢
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多