How can I achieve having an UITableView set the insets of its cells without setting the inset on the entire table?

Justin Vallely picture Justin Vallely · Dec 11, 2017 · Viewed 8k times · Source

I have a UITableViewCell with content that extends to all of its edges. I use this cell in multiple tables with where it needs varying insets. (i.e. one table will have it inset from the left by 8 points, another will have it inset 20 points. I'd like to indent the cell in the parent UITableView code (maybe cellForRow?) without indenting the entire table.

This obviously sets it for the entire table, but I'd like to do it on a cell-by-cell basis as needed:

tableView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: -20)

This is having no effect:

tableViewCell.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: -20)

Nor is this working (even though it's on the cell itself):

override func layoutMarginsDidChange() {
    super.layoutMarginsDidChange()

    self.layoutMargins = UIEdgeInsets(top: 0, left: 20 bottom: 0, right: -20)
}

Answer

Justin Vallely picture Justin Vallely · Dec 11, 2017

Make sure you constrain to margins in the xib/storyboard. Then set the following on the UITableViewCell:

var topInset: CGFloat = 0
var leftInset: CGFloat = 0
var bottomInset: CGFloat = 0
var rightInset: CGFloat = 0

override func layoutMarginsDidChange() {
    super.layoutMarginsDidChange()
    self.layoutMargins = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
}

Then in the tableView cellForRowAt, use the following:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let browseTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BrowseTableViewCell", for: indexPath) as! BrowseTableViewCell

    browseTableViewCell.leftInset = 20
    browseTableViewCell.rightInset = 20
    // Top and bottom insets are not needed. They will default to 0.

    return browseTableViewCell
}