我在一个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
}
答案 0 :(得分:2)
变化:
_tapLabel:(id)sender
为:
_tapLabel:(UITapGestureRecognizer *)gesture
然后通过以下方式获取点击视图:
gesture.view
更新颜色将是:
gesture.view.backgroundColor = [UIColor redColor];