Change UITableView cell.detailTextLabel colour on highlight - iphone

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

Related

Creating a custom accessoryView in the UITableViewCell's setSelected and setHighlighted methods is causing the rest of my code to be ignored

In my UITableViewCell subclass, I'm overriding the setHighlighted and setSelected methods to change the look of the cell when it's selected, but whenever I set the accessoryView property in either of the methods, all my other code that changes the font and shadow colors is ignored entirely.
For example, using this code will change the text color of the text and detail labels
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
}
}
But the moment I add the custom accessoryView to the mix, all the other code is ignored, however the accessoryView image does appear.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
self.accessoryView =
[[UIImageView alloc] initWithImage:styleImage(#"/icons/disclosure_selected.png")];
}
}
Is there something I'm doing wrong? How can I properly customize the accessoryView and the rest of the cell's code during the selected and highlighted states?
I ended up just creating my own UIImageView and hiding the built in one.
I think accessoryView is a UIButton not a UIImageView...
Try this :
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"/icons/disclosure_selected.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26); //You can change it
accessory.userInteractionEnabled = YES; //You can change it too
self.accessoryView = accessory;
Hope it'll help

How to make a UITableView as transparent one?

In my application, i am presenting an UITableViewController class (Grouped style) to the user for the particular scenario. I want to show the table view's cells only. The background should be transparent so that the user can see the previous page.
I tried with the following codes in viewDidLoad method of the table view controller. But they are not working..
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
Is it possible to show the cells with a dimmed background?
Thanks in Advance
I use: (in viewDidLoad)
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
Then in cellForRowAtIndexPath:
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:.55];
Not sure if this is what you wanted, but it gives you the option to make everything clear. (obviously the alpha would change for your case, but that measures theo pacity of the cells)
Just altering the tableView doesn't help. You have to mess with the cells also. Inside the cellForRowAtIndexPath:(NSIndexPath *)indexPath method, put this before the return statement:
UIView * bgView =[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bgView.backgroundColor = [UIColor clearColor];
cell.backgroundView = bgView;

Change UITableView Cell Selection Style to Red

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

changing the background color of custom cell

It looks simple thing but i dont know why i am not able to change the background color of a custom tag on which i am working. Please check my code below for the custom cell.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor orangeColor];
self.textLabel.text = #"lklklk";
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.contentView.backgroundColor = [UIColor blackColor];
self.accessoryView.backgroundColor = [UIColor blackColor];
}
return self;
}
The cell is only displaying the above text with white backgroud
Thanks in advance
I've had to deal with this challenge myself while working on Spring Cleaning
Here's how I solved the issue:
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor blackColor];
view.opaque = YES;
self.backgroundView = view;
[view release];
UITableViewCells have a backgroundView which you need to hide in order to set the backgroundColor:
[self.backgroundView setHidden:YES];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;

Why does this statement about UIColor whiteColor evaluate to false?

Just testing something out....I'm trying to get the background colour of my view to switch when I shake it....but only if it is currently a certain colour.
-(void)viewDidLoad{
self.view.backgroundColor = [UIColor whiteColor];
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if(event.subtype == UIEventSubtypeMotionShake)
{
[self switchBackground];
}
}
-(void)switchBackground{
//I can't get the if statement below to evaluate to true - UIColor whiteColor is
// returning something I don't understand or maybe self.view.backgroundColor
//is not the right property to be referencing?
if (self.view.backgroundColor == [UIColor whiteColor]){
self.view.backgroundColor = [UIColor blackColor];
}
}
You are comparing pointers here, not color values. Use -isEqual method for objects comparison:
if ([self.view.backgroundColor isEqual:[UIColor whiteColor]])
...
Note that view's backgroundColor property is defined with copy attribute so it does not preserve the pointer to color object. However the following simple example will work:
UIColor* white1 = [UIColor whiteColor];
if (white1 == [UIColor whiteColor])
DLogFunction(#"White"); // Prints
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
if([self.view.backgroundColor isEqual:[UIColor whiteColor]]){
self.view.backgroundColor=[UIColor blackColor];
} else {
self.view.backgroundColor=[UIColor whiteColor];
}
// your background will be black now
}