Why does UITableView ignore separatorColor for "extra" separators with custom separatorInset?
Asked Answered
M

2

11

If I set a red separatorColor on a table view in iOS 7 and also set the separatorInset to UIEdgeInsetsZero or any other custom inset value, all additional or "extra" rows have default colored separators. How can I fix this?

The last sentence of the documentation on separatorInset implies that it somehow controls the extra records, but I don't see how:

In iOS 7 and later, cell separators do not extend all the way to the edge of the table view. This property sets the default inset for all cells in the table, much like rowHeight sets the default height for cells. It is also used for managing the "extra" separators drawn at the bottom of the plain style tables.

Set the separator in viewDidLoad:

self.tableView.separatorColor = [UIColor redColor];

And you get this:

enter image description here

And when you set the separatorInset and a color:

self.tableView.separatorInset = UIEdgeInsetsZero;    // <- any custom inset will do
self.tableView.separatorColor = [UIColor redColor];

You get this:

enter image description here

Why does this happen and how can I make all the separators red and set all the separator insets to zero? Is this a bug?

Musquash answered 1/10, 2013 at 20:39 Comment(2)
The same problems seems to happen with any custom separatorInset, not just UIEdgeInsetsZero. Fortunately the accepted solution works with any custom separatorInset as well. Thanks!Preclinical
Good catch. I updated the question to include this.Musquash
M
21

Solved it, but I can't explain why.

Swap the order of the two statements. Set the color first, then the inset:

self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorInset = UIEdgeInsetsZero;

Everything works:

enter image description here

Musquash answered 1/10, 2013 at 22:36 Comment(5)
Yes. Can you tell us why your answer is not a hack also? It does not explain why the color is not respected.Musquash
@AustinMarusco it's not a hack, it's iOS bug apparently that it doesn't repaint separators if color changed after new style applied.Augmented
So you're saying, in addition to the code in my answer, you must add code to cellForRowAtIndexPath, then it is not a hack? How is adding redundant calls to separatorInset not a hack?Musquash
I will admit that both answers are hacks, but I think this one is worse. Changing the ordering of the two statements should not change the outcome unless the screen is rendered again during the current run-loop(using layoutSubviews). Overall, if someone was coming in afterward to edit your code, they would break it more easily if ordering mattered for simple UI properties.Gitlow
Solved by just adding a comment above the code, then.Furbish
G
2

Try adding this to cellForRowAtIndexPath. As well as setting the tableView's separatorInsets.

cell.separatorInset = UIEdgeInsetsZero;

From the UITableViewCell iOS Docs:

@property (nonatomic) UIEdgeInsets separatorInset

The inset values for the cell’s content.

You can use this property to add space between the current cell’s contents and the left and right edges of the table. Positive inset values move the cell content and cell separator inward and away from the table edges. Negative values are treated as if the inset is set to 0.

Gitlow answered 9/4, 2014 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.