Change text color in custom UITableViewCell iphone

I have a custom cell, and when the user selects this 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 
+4
source share
5 answers

In your didSelectRowAtIndexPath method didSelectRowAtIndexPath 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]; 
+5
source

Since you are using a custom table cell, you can implement code to set label colors by implementing the setSelected and setHighlighted methods in your custom UITableViewCell. This will capture all state changes from the selection, although there are some complicated cases, for example, when setHighlighting is called with NO, when you select and drag outside the cell after it has already been selected. Here is the approach I used that I believe sets the color correctly 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]; } 
+13
source

In tableView:didSelectRowAtIndexPath: write the pointer path for the selected cell. In tableView:cellForRowAtIndexPath: just check if it matches your saved pointer path and set the text color accordingly. I'm not quite sure if the cell will be redrawn after the selection, so you might need reloadData after the selection, but it should be fast enough if you cache cells / follow normal cell drawing practice.

Or use reloadRowsAtIndexPaths:withRowAnimation with a UITableViewRowAnimationNone . But then you will need to follow the last cell that you drew in the gray text and redraw it in a simple style.

0
source

The way I sorted this out was in the "didSelectRowAtIndexPath:" loop through all the tableView submatrices and set the default color for all text labels as follows:

 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]]; 
0
source

It is very simple, you just need to use this method (below), here I set the color of the alternative cells

 -(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 the cellForRowAtIndexPath: method to change cellText recipes ..

0
source

Source: https://habr.com/ru/post/1310187/


All Articles