3

I have a UITableViewCell, drawn in Storyboard.

However, running it on simulator, it seems that there is white line in each cell divider as well header-cell divider.

A is Header in UITableViewCell

A11 - A13 is content in UITableViewCell

enter image description here

How to remove it? It seems I can't find any answer out there.

Update:

Solution here only moves the divider color to left with no margin and keeps the header divider persists. Is there any way to remove the white color for both header and cell without moving the divider line?

Community
  • 1
  • 1
Rendy
  • 5,572
  • 15
  • 52
  • 95
  • 1
    Possible duplicate of [hide separator line on one UITableViewCell](http://stackoverflow.com/questions/8561774/hide-separator-line-on-one-uitableviewcell) – luk2302 Oct 31 '15 at 15:48
  • I know there are many questions about this but I haven't found answers that give me my desired result. – Rendy Oct 31 '15 at 16:10

2 Answers2

2

You could simply choose to set Separator for the tableview to "None". Then subclass the UITableviewCell and create the border by adding a CALayer as a sublayer to the cell at awakeFromNib.

0

Using Swift 4.X and adopting the fastest hacking-method, you can improve code using extensions:

    extension UITableViewCell {

       var isSeparatorHidden: Bool {
        get {
            return self.separatorInset.right != 0
        }

        set {
            if newValue {
                self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0)
            } else {
                self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
            }
        }
    }
}

Then, when you configure cell:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
    switch indexPath.row {
       case 3:
          cell.isSeparatorHidden = true
       default:
          cell.isSeparatorHidden = false
    }
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    if cell.isSeparatorHidden { 
       // do stuff
    }
}
yo2bh
  • 1,356
  • 1
  • 14
  • 26