Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background selected color storyboard static cells

I have an app with storyboard. In one scene I have a tableview with statics cell content. Is possible to change the background selected color to another color out of the default options (blue and gray)?

I know If I can change cell background color in forRowAtIndexPath but in this case I haven't any datasource function from tableview. I'm sure that it is possible from IB or another function that I can modify...

Thanks in advance!

like image 707
jcamacho Avatar asked Jan 13 '12 12:01

jcamacho


2 Answers

You don't need to write neither a single line of code to accomplish that. You can do it all using the storyboard. Just do that:

  1. Add a UIView to your UITableViewCell and link it to the selectedBackgroundView property of the cell (to find this property, you will need to drag the line from the "New Reference Outlet" and release it over the desired UITableViewCell)
  2. Change the color of the UIView to the desired color of the selected state

You can do the same thing with the backgroundView property. Also, you can use a UIImageView to use a image, instead of the single color background of a UIView.

Here is a sample file using a custom UIViewController instead of a UITableViewController, but it works on both: http://uxp.com.br/downloads/CustomCell.zip

like image 141
Leandro Alves Avatar answered Oct 03 '22 12:10

Leandro Alves


I had the same problem. The solution has two parts:

1) getting the cells, look at this. 2) changing the background color: you must create a UIView with the background color you need and set it as the selectedBackgroundView of the cell

For instance, I used this code in the viewDidLoad of the uitableviewcontroller:

UIView *backgroundSelectedCell = [[UIView alloc] init];
[backgroundSelectedCell setBackgroundColor:[UIColor colorWithRed:130/256.0 green:169/256.0 blue:171/256.0 alpha:1.0]];

for (int section = 0; section < [self.tableView numberOfSections]; section++)
    for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
    {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];

        [cell setSelectedBackgroundView:backgroundSelectedCell];
    }  
like image 45
Carlos Avatar answered Oct 03 '22 11:10

Carlos