Question

I have a tableview and the tableview is filled with items from a .plist.

I then use this method to add functionality to when something is pressed:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) { //do something
}

if (indexPath.row == 1) { //do something
}

So on and so forth

I've set a font color with in IB / The storyboard however I'd like to change the color of the text when it's being pressed. So basically I want it to act like a UIButton when it's pressed.

And of course not all text should be change when one cell is pressed, only the color of the text in the current cell that is being pressed.

Was it helpful?

Solution

you could change everything in the selected cell

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.font=[UIFont fontWithName:@"Arial"];
 cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
}

-(void)tableView:(UITableView *)tableView deSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.font=[UIFont fontWithName:@"Arial"];
 cell.contentView.backgroundColor = //default color;
}

OTHER TIPS

UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


 if (indexPath.row == 0) 
 { 
   cell.labelText.color = [UIColor redColor];
 }

 if (indexPath.row == 1) 
 { 
 cell.labelText.color = [UIColor greenColor];
 }
}

One option (I assume you have a UITableViewCell subclass MyTableViewCell, adjust as needed):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) { //do something
}

if (indexPath.row == 1) { //do something
}

MyTableViewCell *cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
// Adjust your cell contents as needed

Also add deselection delegate method to revert changes:

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
 MyTableViewCell *cell = (MyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    // Adjust your cell contents back to normal     

Another option would be to override setSelected:animated: and setHighlighted:animated in your UITableViewCell subclass and make appropriate changes to cell elements there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top