Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
}
like image 507
Justin Vallely Avatar asked Dec 11 '17 20:12

Justin Vallely


People also ask

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

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
}
like image 57
Justin Vallely Avatar answered Oct 28 '22 19:10

Justin Vallely