Brodie
Brodie

Reputation: 3506

changing text color in custom UITableViewCell iphone

I have a custom cell and when the user selects that cell, I would like the text in the two UILabels to change to light gray.

ChecklistCell.h:

#import <UIKit/UIKit.h>


@interface ChecklistCell : UITableViewCell {
    UILabel *nameLabel;
    UILabel *colorLabel;
    BOOL selected;


}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *colorLabel;



@end

ChecklistCell.m:

#import "ChecklistCell.h"


@implementation ChecklistCell
@synthesize colorLabel,nameLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
    [nameLabel release];
    [colorLabel release];
        [super dealloc];
}


@end

Upvotes: 3

Views: 11909

Answers (5)

mavericks
mavericks

Reputation: 1589

It's very simple, You just need to use this(below) method, here i'm setting alternate cell color

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2 == 0) 
    {
         tCell.cellLabel.textColor = [UIColor grayColor];
    }
    else
    {
         tCell.cellLabel.textColor = [UIColor blackColor];
    }
}

PS- no need to use cellForRowAtIndexPath: method for changing cellText Propties..

Upvotes: 0

Matt R
Matt R

Reputation: 804

Since you are using a custom table cell, you can implement code to set the label colors by implementing the setSelected and setHighlighted methods in your custom UITableViewCell. This will capture all state changes from selection, although there are some tricky cases, like when setHighlighting is called with NO when you select and drag outside the cell after it was already selected. Here is the approach I used, which I believe sets the color appropriately in all cases.

- (void)updateCellDisplay {
    if (self.selected || self.highlighted) {
        self.nameLabel.textColor = [UIColor lightGrayColor];
        self.colorLabel.textColor = [UIColor lightGrayColor];
    }
    else {
        self.nameLabel.textColor = [UIColor blackColor];
        self.colorLabel.textColor = [UIColor blackColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    [self updateCellDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self updateCellDisplay];
}

Upvotes: 13

jodm
jodm

Reputation: 2617

The way I sorted this was in "didSelectRowAtIndexPath:" loop through all subviews of tableView and set all the text labels to the default color like this:

for(CustomTableViewCell *tableCell in [tableView subviews]) {
    if([tableCell isKindOfClass:[UITableViewCell class]]) {
        [tableCell.textLabel setTextColor: [UIColor blackColor]];
    }
}

.... then set the selected table cell to white.

[[tableView cellForRowAtIndexPath:indexPath].textLabel setTextColor: [UIColor whiteColor]];

Upvotes: 0

Ben Williams
Ben Williams

Reputation: 4695

In your didSelectRowAtIndexPath method, make a call to get the current cell and update accordingly:

CheckListCell* theCell = (CheckListCell*)[tableView cellForRowAtIndexPath:indexPath];
theCell.nameLabel.textColor = [UIColor lightGrayColor];
theCell.colorLabel.textColor = [UIColor lightGrayColor];

Upvotes: 5

user189804
user189804

Reputation:

In tableView:didSelectRowAtIndexPath: record the index path of a selected cell. In tableView:cellForRowAtIndexPath: just check if it matches your saved index path, and set the text color accordingly. I'm not completely sure if the cell is be redrawn after a selection so you may need to reloadData following selection but it should be fast enough if you are caching cells/following normal cell drawing best practise.

Or use reloadRowsAtIndexPaths:withRowAnimation with UITableViewRowAnimationNone. But you would then need to keep track of the last cell you drew in gray text and redraw it in plain style also.

Upvotes: 0

Related Questions