0

I've a UITableView with some sections. I want that just one section had separator lines for rows.

I tried this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ... if indexPath.section == 1 { cell.separatorInset = UIEdgeInsetsMake(0, 1, 0, 0) // With other values too. } ... return cell } 

but nothing happens.

4
  • try cell.separatorInset = UIEdgeInsetsZero for all the sections you dont want the separator for so basically if indexPath.section != 1 { cell.separatorInset = UIEdgeInsetsZero } Commented Sep 27, 2018 at 13:00
  • add a custom view of height 1 with width equal to cell width. show in section 1 and hide in else .... also don't forget to hide default separator of tableView. Commented Sep 27, 2018 at 13:00
  • none works...... Commented Sep 27, 2018 at 14:19
  • @Augusto - Add 4 extra spaces at the beginning of a line to indent the length of a tab. Commented Sep 27, 2018 at 15:33

1 Answer 1

1

You can create an extension to UITableViewCell:

extension UITableViewCell { func hideSeparator() { self.separatorInset = UIEdgeInsets(top: 0, left: self.bounds.size.width, bottom: 0, right: 0) } func showSeparator() { self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } } 

So you can use it in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

if indexPath.section == 1 { cell.hideSeparator() } else { cell.showSeparator() } 
Sign up to request clarification or add additional context in comments.

5 Comments

Your code not works to me, but your information was usefully to solve my problem.
You know say to me why line not use all width space? The line leave a little blank space of left side.
@Augusto it's default inset for UITableView in iOS. To remove left space set : tableView.separatorInset = .zero
This works, but the x offset of Title HeaderView is seted to 0.
There is no another methods to do it, so you should arrange your header view offsets by code as you need in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.