When I tap a cell in UITableViewCell the cell turns blue by default. How do I change that? A change to another color or in this sample I am building, get rid of the color completely. I have a table which is just a table, the information is in the cell and it does not lead anywhere when pressed. Just annoying it turns blue if pressed, because the text in the cell is harder to read.
-
possible duplicate of [How to change the blue highlight color of a UITableViewCell?](http://stackoverflow.com/questions/2553746/how-to-change-the-blue-highlight-color-of-a-uitableviewcell) – Shekhar Gupta Aug 02 '13 at 12:46
4 Answers
in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method add this code
cell.selectionStyle = UITableViewCellSelectionStyleNone;

- 21,721
- 8
- 65
- 83
The following possibilities to change the selection style of uitableviewcell according to me.
These are default styles available.
cell.selectionStyle = UITableViewCellSelectionStyleBlue; cell.selectionStyle = UITableViewCellSelectionStyleGray; cell.selectionStyle = UITableViewCellSelectionStyleNone;
But if you want to change the style, you also use the following.
cell.backgroundColor = [UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha]; cell.backgroundColor = [UIColor colorWithPatternImage:(UIImage *)image];
Using Second Method , you can specify background color or image.
For Selected Cell Style what you want to achieve, you can use this.
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:(UIImage *)image] autorelease];

- 1,729
- 15
- 23
Changing the property selectedBackgroundView is correct and the simplest way. I use the following code to change the selection color:
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
or u might use this ..
Here is the most efficient way I have come across to solve this problem, use the willDisplayCell delegate method (this takes care of the white color for the text label background as well when using cell.textLabel.text and/or cell.detailTextLabel.text):
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
When this delegate method is called the color of the cell is controlled via the cell rather than the table view, as when you use:
(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
So within the body of the cell delegate method add the following code to alternate colors of cells or just use the function call to make all the cells of the table the same color.
if (indexPath.row % 2)
{
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];

- 2,904
- 3
- 25
- 37
-
Thanks, perfect if I want to set whatever color I want. But for my sample here the line in the first answer was enough, to get rid of the color. cell.selectionStyle = UITableViewCellSelectionStyleNone; – Lars - Mar 15 '12 at 09:08
Use the following code to remove default color on cell selection :
cell.selectionStyle = UITableViewCellSelectionStyleNone;

- 6,206
- 3
- 30
- 50

- 35,723
- 18
- 170
- 177