Question

I've added 7 labels in one UITableViewCell, added a UITapGestureRecognizer and I've set the tap target-action.

I want that all labels can be tapped and change the tapped label to another backgroundColor.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //here I leave out how cell are added to the UITableView.

    self.repeatAll = [[NSMutableArray alloc] initWithObjects:@"MON", @"TUE",@"WED",@"THU",@"FRI",@"SAT",@"SUN", nil];
    int x =60;
    UILabel *label;
    for (int i =0; i<7; i++) {
        NSString *theText = [self.repeatAll objectAtIndex:i];
        label = [[UILabel alloc] init];
        label.text = theText;
        label.frame = CGRectMake(x+label.frame.size.width, 30.0f, 30.0f, 20.0f);
        x = label.frame.size.width + 5+ x;
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapLabel:)];
        [label addGestureRecognizer:tapGesture];
        [cell addSubview:label];        
    }
}

- (void)_tapLabel:(id)sender {
    //something I don't know how to change the target label. change it's backgroundColor
}
Was it helpful?

Solution

Change:

_tapLabel:(id)sender

to:

_tapLabel:(UITapGestureRecognizer *)gesture

and then get the tapped view via:

gesture.view

updating the color would be:

gesture.view.backgroundColor = [UIColor redColor];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top