skip to Main Content

I was wondering how I would go about adding a separator line under each of my table view cells on Xcode (using swift) I want to make it so that under all of the cells other than the first cell it will add a separator.

Thank you in advance 😀

Update:

Code that used to work for me.

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    
    let result = UIView()
    
    // recreate insets from existing ones in the table view
    let insets = tableView.separatorInset
    let width = tableView.bounds.width - insets.left - insets.right
    let sepFrame = CGRect(x: insets.left, y: -0.5, width: width, height: 0.5)
    
    // create layer with separator, setting color
    let sep = CALayer()
    sep.frame = sepFrame
    sep.backgroundColor = tableView.separatorColor?.cgColor
    result.layer.addSublayer(sep)
    
    return result
}

The code above does the following within the old version however now doesn’t add any extra lines.

Example of what I want:

Screenshot of the result I used

The setup that I have it as:

Setup of the table

2

Answers


  1. Add a view on bottom of uitableview cell and hide and unhide when required in cellForRowAt. Use following constraints for seperator view

    enter image description here

    Login or Signup to reply.
  2. Add a cell with the same width as the other cells and make the height 1~2 according to your demand after every regular cell.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search