ios - 如何更改已点击的 UITableViewCell 中的 UILabel

标签 ios uitableview uilabel uitapgesturerecognizer

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

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

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

最佳答案

改变:

_tapLabel:(id)sender

到:

_tapLabel:(UITapGestureRecognizer *)gesture

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

gesture.view

更新颜色将是:

gesture.view.backgroundColor = [UIColor redColor];

关于ios - 如何更改已点击的 UITableViewCell 中的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390670/

相关文章:

ios - 计算 UILabel 文本大小

ios - UICustom 单元格在 iOS 7 中不显示标签

ios - 从更新函数调用时,自定义 SKShapeNode 未出现在场景中

ios - 在 uitableviewcell swift 中加载 tableview

ios - UITableViewCell 使用自定义 layoutSubviews() 出现在屏幕上后隐藏部分内容

ios - 防止 UITableView 单元格触摸时键盘关闭

ios - Storyboard中带有不同颜色文本的 UILabel

ios - 将 uipagecontrol 连接到我的整个 uicollectionview,而不是单个单元格

ios如何在iOS中实现SocketIO

ios - 将 UIView 添加到 TableViewControllers View