我在一个 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/