【问题标题】:How to change the UILabel in a UITableViewCell that has been tapped如何更改已被点击的 UITableViewCell 中的 UILabel
【发布时间】:2013-12-05 03:10:06
【问题描述】:

我在一个 UITableViewCell 中添加了 7 个标签,添加了一个 UITapGestureRecognizer 并设置了点击目标操作。

我希望可以点击所有标签并将点击的标签更改为另一个背景颜色。

- (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
}

【问题讨论】:

    标签: ios uitableview uilabel uitapgesturerecognizer


    【解决方案1】:

    变化:

    _tapLabel:(id)sender
    

    到:

    _tapLabel:(UITapGestureRecognizer *)gesture
    

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

    gesture.view
    

    更新颜色是:

    gesture.view.backgroundColor = [UIColor redColor];
    

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多