1

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?

Vincia Tanqling
  • 57
  • 1
  • 11

3 Answers3

0

In didSelectRowAtIndexPath

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

This will change the color of label on selected cell.

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
0

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]];
Samir
  • 902
  • 9
  • 23
  • ya ,I have multiple cells. – Vincia Tanqling May 15 '15 at 04:31
  • For multiple cells if want to apply color to all. Follow above steps. Or you can also create custom cell and apply color to each label. – Samir May 15 '15 at 04:33
  • It work!Thanks! But what if i only want selected cell change to green and others still remain the same color? – Vincia Tanqling May 15 '15 at 04:37
  • just create private variable in your class as NSInteger selectedRow. Also update selectedRow value in didSelectRowAtIndexpath as selectedRow = indexPath.row. And in cellForRowAtIndexPath check for selectedRow with indexPath.row and apply color same as you have done in didSelectRowAtIndexPath. Note: Initially selectedRow will have default value i.e 0. So set selectedRow = NSNotFound; so that. It doesn't set greencolor to label for 0th index on initial load – Samir May 15 '15 at 04:43
  • Can you provide me an example? Really appreciate your help!! – Vincia Tanqling May 15 '15 at 04:45
  • 1
    You can do with your own. I think I have explained the solution. I know you are new, but don't make habbit of using readymade code. If you want to learn from basic? Download videos from Stanford CS193p – Samir May 15 '15 at 04:53
  • http://stackoverflow.com/questions/13099782/use-selected-row-in-another-class is this what u means ? – Vincia Tanqling May 15 '15 at 04:55
  • No, That question is about using selectedRow in another class. Here We can create in variable in same class. – Samir May 15 '15 at 04:57
  • Now i am trying to do if (index path == 0) -> setfirstlabel to green(with tag 1000), set secondlabel to white(with tag 1200) else if (index path ==1) -> setfirstlabel to white(with tag 1000),set second label to green(with tag 1200) I click on first label it turn green,but when i clicked on second label, second label turn green but first label still remain white color.Can you tell me why? – Vincia Tanqling May 15 '15 at 05:08
  • Because you have set firstLabel to white for IndePath == 1 – Samir May 15 '15 at 05:13
0
- (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];
    }


}
Vincia Tanqling
  • 57
  • 1
  • 11