views:

355

answers:

3

Hello. 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
A: 

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.

Adam Eberbach
+1  A: 

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];
alku83
this works pefectly except for when I scroll away from the modified cells, then scroll back, the cell is back to it's orig state. So I assume i have to keep track of which cells are modified elsewhere? Im guessing to keep an array of indexPaths which have been modified, then in the cellForRowAtIndexPath method have it check the array for that index path? Or is there ane asier way?
Brodie
and of course, now that im trying to do that I relize there's no easy way to compare two indexPaths....
Brodie
Yes, you should really be updating your datasource at the same time. What I described is really only for getting the instant visual update. I am assuming you have an array of objects which you are using as the datasource for the table? You could add a property to the object to define whether it is selected or not, update this in the didSelectRowAtIndexPath method (as well as the code above), and use it to determine textColor in cellForRowAtIndexPath method. Hope thats clear!
alku83
yes it's clear, but for this table I cant do anything with the datasource like this without redoing everything. I am instead making an aray of indexpaths, and creating a loop that will compare the indexpath with all of the indexpaths in that array. Not sure that will work though. when i try to add an index path to the mutablearray, nothing happens and a NSLOG showing the contents of the array always shows null even after a supposedly successful addObject:indexPath]. is it just not possible to add indexpaths to an array or am i just doing something wrong (again, lol)
Brodie
have you alloc/init'ed the array?
alku83
i got it working but im having trouble figuring out how to compare two index paths, but i will ask a separate question about that thank you for your help
Brodie
alku83
A: 

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]];
jodm
As you can see as I looped through I checked to see if the subview was kind of UITableViewCell and only changed the color if it was. Hope this helps!
jodm