6

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!

jcamacho
  • 818
  • 2
  • 12
  • 25

3 Answers3

18

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

Leandro Alves
  • 2,190
  • 3
  • 19
  • 24
  • great approach! This is the way to work with storyboards, that many of us aren't familiar with, and how they are thought to be worked with. Thanks! – brainray Oct 23 '12 at 23:14
  • 2
    How did you accomplish this without getting this error? http://stackoverflow.com/questions/13090338/runtime-error-setting-backroundview-property-of-uitableviewcell-in-storyboard – Adam Ritenauer Oct 31 '12 at 03:24
  • I'm not using AutoLayout. Maybe that's why I don't get any error. But it looks like a bug to me. Have you filled a bug within Apple? – Leandro Alves Oct 31 '12 at 18:58
  • I was unable to add `UIView` to `UITableViewCell`. The only place it wanted to fit was in `UITableView` itself and that's of no use. – Martin Berger Jan 10 '13 at 09:20
  • 1
    Hi, @MartinBerger. You can drag a UIView to a UITableViewCell using the Storyboard view. If you're having problem to drag it to the proper place, try dragging it using the hierarchy view: put the view anywhere in the Scene and drag it to be child of the cell. – Leandro Alves Jan 10 '13 at 16:47
  • Hello @Leandro. I tried that all before. Couldn't plug it in. Is there an issue if i don't use `UITableViewController`? I have custom `UIViewController` and he holds my `UITableView`. – Martin Berger Jan 12 '13 at 12:06
  • 1
    @MartinBerger, I just added a sample file on my answer. Open it and check it to see what you're missing. And sorry for the delayed answer. – Leandro Alves Jan 15 '13 at 13:51
  • @Leandro: I have dl-ed project and ran it. It works. As for my project, this is strange... I know i had selected cell, several times, and have tried to insert label, view, image view etc. And it didnt work. Now it works. I select a cell and drag an drop label, image view etc... Strange. Thank you for helping me with this. I can discard that .nib file i made as solution, and made this purely in storyboard. – Martin Berger Jan 16 '13 at 11:48
  • Any advantage to set the selectedBackgroundView property? – Raphael Oliveira Apr 19 '13 at 20:10
  • @RaphaelOliveira The advantage is that you don't need to write any code. But, if you need a dynamic background (i.e. change it depending on the content of the cell), I don't see any advantage. – Leandro Alves Apr 22 '13 at 14:22
  • 1
    This approach is great but will not work (layout subviews crash) with auto layout on iOS 6.0. – eladleb Jun 28 '13 at 10:22
9

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];
    }  
Community
  • 1
  • 1
Carlos
  • 1,121
  • 12
  • 26
  • Thanks Carlos... are you spanish? – jcamacho Apr 25 '12 at 16:37
  • This approach probably works. I really don't know how it will behave when you have a bunch of cell and the table scrolls. In dynamic cells, they are cached and discarded when needed. Changing the background during the didLoad method will ensure that the first presenting cells have a correct background color, but may not ensure that the scrolling cells will keep that reference. Anyway, it needs coding, and a static cell is intended to use without the need of coding... I posted an answer that let you accomplish that without coding. – Leandro Alves Sep 21 '12 at 00:41
-2

Here is a solution for those of us using Auto-Layout on X-Code 5+...

For the static cells, you can supposedly change the background color, but it won't work. Each individual cell, however, will automatically have a Content View inside it in IB. If we change the background of this Content View, it changes the cell background.

Just something to add to @Leandro Alves' answer, so we don't have to drag extra UIViews onto our project!

Eichhörnchen
  • 592
  • 2
  • 12
  • 27