0
votes

I am only able to get separator insets change working on the table view level. If I create a custom inset on UITableViewCell or try to remove it, it doesn't work. I have a tableview where in some cells I need a custom separator inset i.e. little indented on left and touching the margin on right and for some cells I don't need separator at all.

If I set bookWorkspaceTableView.separatorStyle = .none in viewDidLoad(), I don't see the separator at all even on the cells where I am trying to show it. If I don't set `bookWorkspaceTableView.separatorStyle = .none' then I see separator even for the cells where I don't want to see.

Below code is where I am not setting separator style on table view to none and I want to hide the default table view separator, but I am still seeing it even with below code... how do I fix it? :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        log.debug("MyPlaces::cellForRowAt \(indexPath.row)")

        if (indexPath.row == 0) {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "allDaySwitchCell") as? LeftLabelRightSwitch {
                cell.selectionStyle = UITableViewCell.SelectionStyle.none
                cell.leftLabel.text = IndoorsLocalizedString.all_day.getLocalizedString()
                cell.leftLabel.textAlignment = .left

                cell.layoutMargins = UIEdgeInsets.zero
                cell.separatorInset = UIEdgeInsets.zero

                // Remove seperator inset
//                if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
//                    cell.separatorInset = .zero
//                }
//                // Prevent the cell from inheriting the Table View's margin settings
//                if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
//                    cell.preservesSuperviewLayoutMargins = false
//                }
//                // Explictly set your cell's layout margins
//                if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
//                    cell.layoutMargins = .zero
//                }

                //cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                return cell
            }
        }

enter image description here

enter image description here

1

1 Answers

0
votes

This is a pain for me too. So i don't want to use the default separator from UITableView. Then i create my own separator. Create an extension for UITableViewCell and set my own.

extension UITableViewCell {
    func addSeperator() {
        let seperator = UIView(frame: CGRect(x: 0, y: contentView.frame.height - 1, width: contentView.frame.width, height: 1))
        seperator.backgroundColor = .seperator
        contentView.addSubview(seperator)
    }
}

In your case, i think you just need options for this function to set the margin.