How can I enable separator only in a specified section?
4 Answers
A bit of a hack and probably not the cleanest solution but you can set the UITableViewCell.separatorInset
value to make the separator "invisible".
For example you could do something like this in a custom UITableViewCell
class (attached to a nib maybe):
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
}
So this would set the right and left co-ordinates of the inset to the same value and in effect create a 0-length inset. If you table has multiple row types, you can use the above in the specific rows to make the separator invisible. For the rows where you want the separator to show, just use the table view's separator setting.

- 471
- 6
- 10
-
2This is by FAR the best solution. Thanks! – Nathan F. Dec 31 '20 at 07:42
Well, if you are using custom cells, you can turn off separatorStyle
on the tableview and add an extra view of height 1 with a light gray background at the bottom of your custom cell's contentView, either add it in your Nib/Storyboard's Prototype Cell or programmatically add it in the cells initialization code.
This would be much cleaner than adding another table in front of the 1st table as suggested in the other answer.

- 6,457
- 2
- 43
- 48
separatorStyle
is an attribute of the table, so you cannot have different separators in different sections of the same table. Therefore, you will have to use multiple tables and configure each one's separator style the way you want.
You could just place the 2nd table in front of the other table and not put anything in the table section that is hidden.
Alternatively, you could put the 2nd table inside a section of the 1st table, and make that section have a single row which contains the 2nd table.

- 12,116
- 8
- 48
- 74
-
No native way - only emulation. And yes, I know it's a table's property. – Artem Oboturov Oct 26 '11 at 08:35
-
Or I can add a fake cell with some predefined color as a background. – Artem Oboturov Oct 26 '11 at 10:45
-
Instruction to what @Artem wrote here: http://stackoverflow.com/questions/4804632/uitableview-separator-line – Oded Ben Dov Oct 15 '12 at 16:37
In which ever section you need separator add tableView.separatorStyle = .singleLine
alter tableView.separatorStyle = .none

- 173
- 1
- 6