Mr Riksson
Mr Riksson

Reputation: 560

Chang color of item in tableview when pressed

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.

Upvotes: 0

Views: 84

Answers (4)

codercat
codercat

Reputation: 23271

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;
}

Upvotes: 1

Mark Woollard
Mark Woollard

Reputation: 159

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.

Upvotes: 0

Pancho
Pancho

Reputation: 4143

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

Upvotes: 0

Sunny Shah
Sunny Shah

Reputation: 13020

UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;

Upvotes: 0

Related Questions