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)
}
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With