如何更改已被轻击的UITableViewCell中的UILabel

时间:2013-12-05 03:10:06

标签: ios uitableview uilabel uitapgesturerecognizer

我在一个UITableViewCell中添加了7个标签,添加了一个UITapGestureRecognizer并设置了tap target-action。

我希望可以点击所有标签并将点按的标签更改为另一个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
}

1 个答案:

答案 0 :(得分:2)

变化:

_tapLabel:(id)sender

为:

_tapLabel:(UITapGestureRecognizer *)gesture

然后通过以下方式获取点击视图:

gesture.view

更新颜色将是:

gesture.view.backgroundColor = [UIColor redColor];