Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Cell selected Color?

I have created a custom UITableViewCell. The table view is showing data fine. What I am stuck in is when user touches cell of tableview, then I want to show the background color of the cell other than the default [blue color] values for highlighting the selection of cell. I use this code but nothing happens:

cell.selectedBackgroundView.backgroundColor=[UIColor blackColor]; 
like image 288
Mc.Lover Avatar asked Jan 04 '10 10:01

Mc.Lover


People also ask

How to change UITableView background color?

Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.

How to remove tableview selection color in Swift?

Inside the cellForRowAt indexPath method, on the table view cell, set the selectionStyle to . none .


1 Answers

No need for custom cells. If you only want to change the selected color of the cell, you can do this:

Objective-C:

UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor redColor]; [cell setSelectedBackgroundView:bgColorView]; 

Swift:

let bgColorView = UIView() bgColorView.backgroundColor = UIColor.red cell.selectedBackgroundView = bgColorView 
like image 175
Maciej Swic Avatar answered Oct 12 '22 18:10

Maciej Swic