Search code examples
objective-cuilabeluitableviewswrevealviewcontrollerviewwithtag

How to change UILabel text color on Custom UITable cell?


I've created a table view controller on storyboard. I want to change UILabel text color to green when i clicked on the selected row.

I'm trying something like this, but it's not working:

- (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];


}

I've drag label in table cell and set the identifier to home,stamp,scanner... and change tag to 1000.

Can anyone tell me why the label text color still remain the same and provide a solution for me?


Solution

  • If you have multiple labels in cell, then you need to set its color separately To get cell which is loaded in memory, use

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    remove if(indexPath.row == 0) condition to apply color on every cell.

    and loop in cell.contentView for labels

    (If multiple label, You can also get it by tag, But apply unique tag to each label and get every label with tag)

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

    For Single label above loop will work, but its easier to get with tag

    UILabel *currentLabel= (UILabel*)[cell.contentView viewWithTag:1000];
    [currentLabel setTextColor:[UIColor greenColor]];