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

时间:2014-04-24 10:36:09

标签: ios uitableview uitapgesturerecognizer didselectrowatindexpath

我已经看过关于这个主题的其他问题/问题,但是我不能让我的工作。

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

}

1 个答案:

答案 0 :(得分:3)

尝试将其添加到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];


希望这有助于你