171

I have added a UITableView in IB and set the "delegate" and "datasource" and all is working well. What I wanted to do next was change the separator color, but the only way I could find to do this was to add the method to one of the delegate callbacks, is there a better place I should put this?

I don't have this at the moment but I was thinking that maybe I need to add an "iVar" from my controller that I can link to the UITableView in IB and then set separator color in the viewDidload?

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setSeparatorColor:[UIColor blackColor]];
    return 65;
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • Adding the variable and hooking it up in IB is how I've always done it. – s1mm0t Jul 29 '10 at 16:52
  • Thats what I was thinking. I was doing a tutorial they did not use and iVar, they just relied on the delegate and datasource callbacks. However they never accessed the separator color so maybe thats why. – fuzzygoat Jul 29 '10 at 17:09

6 Answers6

361
- (void)viewDidLoad
{
   [self.tableView setSeparatorColor:[UIColor myColor]];
}

You'll need the self. to access it, remember.

Swift 4.2

tableView.separatorColor = UIColor.red
starball
  • 20,030
  • 7
  • 43
  • 238
Helen
  • 3,951
  • 1
  • 16
  • 7
  • Hi Helen, does that access it without the need to add a @property, it looks like it does? – fuzzygoat Jul 29 '10 at 18:30
  • 1
    This is true. You are subclassing UITableViewController, which declares it as a property. It therefore inherits the accessor/setter methods of the superclass, so you can set it accordingly. However, you cannot access the instance variable directly (probably a good thing). The reason why you can set it in a delegate method is because it is a parameter to the method called. – Helen Jul 29 '10 at 19:06
  • 2
    Or if you like: self.tableView.separatorColor = UIColor.clearColor; – bbrame Sep 10 '13 at 17:52
  • 2
    Not particularly part of the answer, but adding `[super viewDidLoad];` to the code snippet would be nice. – totocaster Oct 02 '17 at 00:48
  • This worked, but it has more cells than needed. If I have 4 cells, how do I not have a ton of extra empty cells below? – Mason Ballowe Jun 10 '21 at 23:19
59

Now you should be able to do it directly in the IB.

Not sure though, if this was available when the question was posted originally.

enter image description here

Myxtic
  • 5,679
  • 5
  • 48
  • 69
24

Swift version:

override func viewDidLoad() {
    super.viewDidLoad()

    // Assign your color to this property, for example here we assign the red color.
    tableView.separatorColor = UIColor.redColor()
}
Elijah
  • 8,381
  • 2
  • 55
  • 49
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
12

Try + (instancetype)appearance of UITableView:

Objective-C:

[[UITableView appearance] setSeparatorColor:[UIColor blackColor]]; // set your desired colour in place of "[UIColor blackColor]"

Swift 3.0:

UITableView.appearance().separatorColor = UIColor.black // set your desired colour in place of "UIColor.black"

Note: Change will reflect to all tables used in application.

Nitesh Borad
  • 4,583
  • 36
  • 51
5

Swift 3, xcode version 8.3.2, storyboard->choose your table View->inspector->Separator.

Swift 3, xcode version 8.3.2

Community
  • 1
  • 1
Giang
  • 3,553
  • 30
  • 28
4

If you just want to set the same color to every separator and it is opaque you can use:

 self.tableView.separatorColor = UIColor.redColor()

If you want to use different colors for the separators or clear the separator color or use a color with alpha.

BE CAREFUL: You have to know that there is a backgroundView in the separator that has a default color.

To change it you can use this functions:

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if(view.isKindOfClass(UITableViewHeaderFooterView)){
            var headerView = view as! UITableViewHeaderFooterView;
            headerView.backgroundView?.backgroundColor = myColor

           //Other colors you can change here
           // headerView.backgroundColor = myColor
           // headerView.contentView.backgroundColor = myColor
        }
    }

    func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        if(view.isKindOfClass(UITableViewHeaderFooterView)){
            var footerView = view as! UITableViewHeaderFooterView;
            footerView.backgroundView?.backgroundColor = myColor
           //Other colors you can change here
           //footerView.backgroundColor = myColor
           //footerView.contentView.backgroundColor = myColor
        }
    }

Hope it helps!

ezefire
  • 782
  • 5
  • 18