如何更改自定义 UITable 单元格上的 UILabel 文本颜色?

How to change UILabel text color on Custom UITable cell?

我在故事板上创建了一个 table 视图控制器。我想在单击所选行时将 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];


}

我已将标签拖入 table 单元格并将标识符设置为 home、stamp、scanner...并将标签更改为 1000。

谁能告诉我为什么标签文本颜色仍然保持不变并提供解决方案?

didSelectRowAtIndexPath

 UITableViewCell *cell=(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath.row];
cell.(whatever your label property).textColor=[UIColor greenColor];

这将更改所选单元格上标签的颜色。

如果单元格中有多个标签,则需要分别设置其颜色 要获取加载到内存中的单元格,请使用

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

删除 if(indexPath.row == 0) 条件以在每个单元格上应用颜色。

并在 cell.contentView 中循环以获得标签

(如果有多个label,也可以通过tag获取,但是对每个label应用唯一的tag,获取每个有tag的label)

for (id thisLabel in cell.contentView.subviews) {
    if ([thisLabel isKindOfClass:[UILabel class]]) {
        UILabel *currentlabel = thisLabel;
        [currentlabel setTextColor:[UIColor greenColor]];
    }
}

For Single label above loop 将起作用,但使用标签更容易获得

UILabel *currentLabel= (UILabel*)[cell.contentView viewWithTag:1000];
[currentLabel setTextColor:[UIColor greenColor]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    if (selectedRow == 0) {
        UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
        [homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
    }else if (selectedRow == 3){
        UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
        [settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
    }
    if(selectedRow!= indexPath.row){
        UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
        [homeLabel setTextColor:[UIColor whiteColor]];
        UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
        [stampLabel setTextColor:[UIColor whiteColor]];
        UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
        [scannerLabel setTextColor:[UIColor whiteColor]];
        UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
        [settingLabel setTextColor:[UIColor whiteColor]];

    }

    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    selectedRow = indexPath.row;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(indexPath.row == 0){
        UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
        [homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        [self.tableView reloadData];
    }
    if(indexPath.row == 1){
        UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
        [stampLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        [self.tableView reloadData];

    }
    if(indexPath.row == 2){
        UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
        [scannerLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        [self.tableView reloadData];
    }
    if(indexPath.row == 3){
        UILabel *settingLabel = (UILabel*)[cell.contentView viewWithTag:1400];
        [settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        [self.tableView reloadData];
    }


}