7

How can I enable separator only in a specified section?

Artem Oboturov
  • 4,344
  • 2
  • 30
  • 48

4 Answers4

5

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.

srinij
  • 471
  • 6
  • 10
4

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.

CodeBrew
  • 6,457
  • 2
  • 43
  • 48
3
 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.

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
1

In which ever section you need separator add tableView.separatorStyle = .singleLine alter tableView.separatorStyle = .none

Sudhakar Varma
  • 173
  • 1
  • 6