سؤال

I've seen the other examples/questions asked on SO in regards to the topic but I cannot get mine to work.

I have a custom table view cell, to which I have added a label. The user is to be able to touch the label in order to change its colour. However when I tap the label it calls didSelectCellAtIndexPath and does not register the tap.

I would like the label to get priority, therefore if the label is pressed it is to change the colour and not call didSelectCellAtIndexPath.

But if I select an area outside of the label then it is to act like a normal cell and call didSelectCellAtIndexPath.

Here is my custom cell:

@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");

}
هل كانت مفيدة؟

المحلول

try by adding it to contentView, for any view that u are adding to table cell add it to cell's contentView

for example

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


hope this helps u

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top