我在情节提要上创建了一个表格视图控制器。单击所选行时,我想将 UILabel 文本颜色更改为绿色。
我正在尝试这样的事情,但它不起作用:
- (void)viewDidLoad {
[super viewDidLoad];
menuItems = @[@"home", @"stamp", @"scanner", @"settings"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
NSLog(@"cell %@",[cell.contentView viewWithTag:1000]);
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0){
UILabel *menu= (UILabel*)[cell.contentView viewWithTag:1000];
menu.textColor = [UIColor greenColor];
NSLog(@"cell clicked: %@",[cell.contentView viewWithTag:1000]);
}
//[cell.textLabel setTextColor:[UIColor greenColor]];
// [self setCellColor:[UIColor greenColor] ForCell:cell];
[self.tableView reloadData];
}
我已经在表格单元格中拖动标签并将标识符设置为 home、stamp、scanner... 并将标签更改为 1000。
谁能告诉我为什么标签文本颜色仍然保持不变并为我提供解决方案?