1

I'm already searching for two days about this and can't find my answer...

I have a custom uitableviewcell, then I need to implemenent custom color, when cell is selected.

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];

Doesn't work for me. Or if it works, the background color is clear, and I don't get whats wrong. It seems that I don't know how to properly connect my custom cell in Interface builder. I can create a custom cell but the only thing is that I don't know how to make the selection work. So if anyone could help me I would be really thankful. Or maybe someone knows a tutorial about this?

Thanks in advance!

Unome
  • 6,750
  • 7
  • 45
  • 87
Lukas
  • 2,515
  • 4
  • 28
  • 37
  • There's a [similar question](http://stackoverflow.com/questions/1998775/uitableview-cell-selected-color/) to this on stackoverflow, hope you'll find what you need there – emmo Jul 11 '12 at 08:06
  • thanks but i didn't find something that would help.. – Lukas Jul 11 '12 at 18:27

5 Answers5

6

Hope the below Code must work. You need to copy paste this code in the customCell implementation file.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

UIView *backgroundView = [[UIView alloc] initWithFrame:self.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor colorWithRed:143/255.f green:141/255.f blue:147/255.f alpha:1.0]];
[self setSelectedBackgroundView:backgroundView];
[backgroundView release];
}
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
  • Thanks this is good, but unfortunately it doesn't work :// I'm almost certain that i have mesed up connections in IB, i would love to se custom celles with selection implemented somewhere.. Maybe you know some open source project where i could find an example? – Lukas Jul 11 '12 at 10:49
4

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

UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
Shorhashi
  • 670
  • 5
  • 8
  • i have tried it like that, but then the selection somehow goes on top of my custom cell, and imageview and label goes under it.. – Lukas Jul 11 '12 at 08:43
3

@Pandey_Laxman is right. Simply using cell.seletedBackgroundView won't work because either the backgroundView and the selectedBackgroundView of UItableViewCell is nil by default. You need to init a UIView before set it as a cell's selectedBackgroundView. see the definition of selectedBackgroundView()

BabyPanda
  • 1,562
  • 12
  • 18
1

Finnaly i have found the answer to my question..

I've made very stupid mistake.. So first thing you need to set the files owner class as NSObject to your customcell in IB, then your uitableviewcell class as your custom cell, in my case "myCustomDisplayCell"..

Futhermore from bottom to top (views in IB), I've added a uiview and hooked up with cells backgroundview, on top i've placed a uiview and hooked up with cells selectedbackgroundview(made view color clear) then finnaly i placed a uilabel uiimageview, and hooked up these right.. (you can add your own things here needed for custom cells). And that's it! Looks kind of easy now..

Thank you all for help!

Lukas
  • 2,515
  • 4
  • 28
  • 37
0

You set it in the custom cell class as following (Swift 4):

import UIKit

class CustomTableViewCell: UITableViewCell { 

... 

 override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if(selected) {
            self.backgroundColor = UIColor(named: "BrilliantAzure")
        } else {
            self.backgroundColor = UIColor.white
        }
    }

...

As result, no need to create an additional view for this specific task.

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53