I have subclassed UITableViewCell and I have the following code:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected)
self.textField.textColor = [UIColor whiteColor];
else
self.textField.textColor = [UIColor blackColor];
[super setSelected:selected animated:animated];
}
Basically I'm having only a UITextField in my cell but the color of the label won't change automatically to white so I need some kind of a way to change it on highlight. Any ideas?
You should use cell.textLabel.highlightedTextColor and didn't change it in setSelected:
I believe that IS the proper way to set the text color on aUITextField. But what I'm wondering is: is self.textField properly wired up to the correct instance? (via Interface Builder/StoryBoard or in code)
Try adding this code at the top of your setSelected method and look at the console while running your app:
NSLog(#"self.textField = %#", self.textField);
In your cellForRowAtIndexPath
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and in your subviews have some thing like this
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected)
self.textLabel.textColor=[UIColor blackColor];
else
self.textLabel.textColor=[UIColor greenColor];
}
Related
How can I make a custom sublayer in a UITableViewCell disappear in setSelected:animated:?
Context:
I've added a custom background layer to my UITableViewCell by adding a CAGradientLayer inside of drawRect:, like so:
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
[[self providerLabel] setTextColor:kOUBlue];
[self addGrayLayer];
}
- (void)addGrayLayer{
[[[self contentView] layer] insertSublayer:[self grayGradientLayer] atIndex:0];
}
- (CAGradientLayer *) grayGradientLayer{
if (!_grayGradientLayer) {
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *white = [UIColor colorWithWhite:1 alpha:2];
UIColor *gray = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
[gradient setColors:#[(id)white.CGColor, (id)gray.CGColor]];
[gradient setFrame:[self bounds]];
_grayGradientLayer = gradient;
}
return _grayGradientLayer;
}
When the user taps on a cell, the blue highlighting doesn't appear. So I've attempted to hide the CAGradientLayer in setSelected:animated: like so:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
[[self grayGradientLayer] setHidden:YES];
}else{
[[self grayGradientLayer] setHidden:NO];
}
[[self contentView] setNeedsDisplay];
}
The blue background appears, but not until the push animation begins. Is there any way for me to make the default blue appear immediately? I've also tried to add a second colored CAGradientLayer and swap them manually, but it seems to me that the cell doesn't redraw immediately after I hide the layer in setSelected:.
I've tried to force the cell to redraw, using setNeedsDisplay with no luck. Any ideas?
After digging around, I found this question, I realized I was using the wrong method. It turns out I had to use the setHighlighted:animated: method.
My new code looks like this:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[[self grayGradientLayer] setHidden:YES];
}else{
[[self grayGradientLayer] setHidden:NO];
}
}
The default colour for text in a UITableView when selected is white. I want to change this to dark grey. I managed to change the main title label text by doing this:
cell.selectedTextColor = [UIColor darkGrayColor];
How can I do this for the detailTextLabel though when it is highlighted/selected?
Thanks
You can subclass UITableViewCell. Then override the setHighlighted:animated Method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.detailTextLabel.textColor = [UIColor lightGrayColor];
} else {
self.detailTextLabel.textColor = [UIColor whiteColor];
}
}
You may end up wanting to override the setSelected:animated method too.
Just use:
cell.detailTextLabel.highlightedTextColor = [UIColor blueColor];
cell.detailTextLabel.textColor = [UIColor blue color];
I got an easy one for you. I'm trying to keep the default behavior when a user is coming back from a view after selecting a tableviewcell. Typically the cell will remain blue for a moment to show the user where that selected row was, but I forget how to customize that. Lets say for simplistic purposes all my cells are green by default and become red when you select them.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor greenColor];
}
}
When the user selects the back button, the cell does not show the user their last selected cell, red in this case. I remember there being a variable I could use to customize that in the controller, but I can't remember what it was. Can somebody point me in the right direction?
Thanks!
If you only want to change the color of the background cell you can create a new view, set it's background color and assign that to the cells selected background view.
UIView *cellSelectedBackgroundView = [[UIView alloc] init];
[cellSelectedBackgroundView setBackgroundColor:[UIColor someColor]];
[cell setSelectedBackgroundView:cellSelectedBackgroundCView];
The property and method I was looking for was:
self.clearsSelectionOnViewWillAppear = YES;
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
Thanks for those who replied.
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
I have a TableView in my app, and I needed to change the height and the color of the Separator. Browsing here in SO helped me to figure out the solution.
So I'm basically adding a UIView in my cell and using this as a "fake" separator:
UIView *colorSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, 53, cell.frame.size.width, 4)];
colorSeparator.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:colorSeparator];
[colorSeparator release];
But now I noticed that when the row is tapped, the selection colour applies to my fake separator. Does anyone know how could avoid it? Thx in advice for your time :)
You can restore color of your separator in setSelected:animated: and setHighlighted:animated: methods of UITableViewCell.
// just edited your function, it was missing a square bracket
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
UIColor *c = [[colorSeparator.backgroundColor retain] autorelease];
[super setHighlighted:highlighted animated:animated];
colorSeparator.backgroundColor = c;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
UIColor *c = [[colorSeparator.backgroundColor retain] autorelease];
[super setSelected:selected animated:animated];
colorSeparator.backgroundColor = c;
}
I need to change the UITableView cell selection style from default blue to red. Can any one help me with this?
You don't need an image or to subclass the cell. Simply do something like the following when creating the cell in your tableView:cellForRowAtIndexPath:
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
You could try setting the cells' selectedBackgroundView.image, per this tutorial. That would give you the option of creating a nice gradient-based selection image, too.
If you sub-class UITableViewCell, you can modify its highlighted and selected UI behavior by overriding following methods.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;
For example:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor blackColor];
}
}
UIImageView *selectedBackground = [[UIImageView alloc] initWithFrame:self.view.frame];
selectedBackground.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:selectedBackground];
you can try something like this