fuzzygoat
fuzzygoat

Reputation: 26223

UITableView, Separator color where to set?

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;
}

Upvotes: 172

Views: 136825

Answers (6)

Helen
Helen

Reputation: 3981

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

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

Swift 4.2

tableView.separatorColor = UIColor.red

Upvotes: 364

King-Wizard
King-Wizard

Reputation: 15694

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()
}

Upvotes: 24

Giang
Giang

Reputation: 3635

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

Swift 3, xcode version 8.3.2

Upvotes: 5

Nitesh Borad
Nitesh Borad

Reputation: 4653

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.

Upvotes: 12

ezefire
ezefire

Reputation: 791

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!

Upvotes: 4

Myxtic
Myxtic

Reputation: 5699

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

Upvotes: 59

Related Questions