I am using UITableViewController
and I am loading customized UITableViewCell
. All elements in the cell and the constraints are configured programmatically. The TableView looks like this
All works perfectly except when deleting a cell. I configured the tableView to fade cells when deleted. However, the cell gives a little jerk movement then it goes to the left instead of fading, and I get auto layout error messages in the Console: "Unable to simultaneously satisfy constraints" for below
"<NSLayoutConstraint:0x600002551a40 UIView:0x7fccbd5286f0.height == 100 (active)>",
"<NSLayoutConstraint:0x6000025505a0 V:|-(16)-[UIView:0x7fccbd5286f0] (active, names: '|':UITableViewCellContentView:0x7fccbd541b10 )>",
"<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>",
"<NSLayoutConstraint:0x6000025605a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fccbd541b10.height == 1.19209e-07 (active)>"
and below
"<NSLayoutConstraint:0x6000025505a0 V:|-(16)-[UIView:0x7fccbd5286f0] (active, names: '|':UITableViewCellContentView:0x7fccbd541b10 )>",
"<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>",
"<NSLayoutConstraint:0x6000025605a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fccbd541b10.height == 1.19209e-07 (active)>"
and that "Will attempt to recover by breaking constraint" for below
<NSLayoutConstraint:0x600002551a40 UIView:0x7fccbd5286f0.height == 100 (active)>
<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>
I broke the console output into parts because I couldn't paste it all together for some reason.
Basically, I have a UIView
called cellView
(the white corner rounded area) inside the contentView
of the cell. And all other elements you see are inside the cellView
. I applied below constraints between the cellView
and contentView
cellView.translatesAutoresizingMaskIntoConstraints = false
cellView.heightAnchor.constraint(equalToConstant: 100).isActive = true
cellView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16.0).isActive = true
cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
cellView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16.0).isActive = true
cellView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16.0).isActive = true
It worked when I tried deactivating all constraints in the cell and its subviews just before deleting the cell. But, when new cells are reused, they didn't have constraints to activate.
How should I handle this case?
One needs to register the nib and then use the cellForRowAt method to use the custom cell. The biggest difference when using a static table view is that you need to do it through Interface builder, and it needs to use a UITableViewController.
As you can see, we need to adjust the height of each cell using UITableViewDelegate protocol to avoid the cells bumping each other. Let’s change the height of the cells. Set ContactsViewController conform to the UITableViewDelegate protocol. class ContactsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ ... }
The biggest difference when using a static table view is that you need to do it through Interface builder, and it needs to use a UITableViewController. You cannot add a normal UITableView in a UIViewController. Now that we have the basics covered we can start implementing the static table view with custom cells.
UITableViewDataSource protocol has two important methods that we MUST implement to populate data on the contactsTableView, they are numberOfRowsInTableView and cellForRowAtIndexPath. Before implementing these methods: • Make contactsViewController conform to the UITableViewDataSource protocol.
You need to lower priority of bottom constraint
let con = cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
con.priority = UILayoutPriority(999)
con.isActive = true
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