Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraints break when programmatically customized UITableViewCell is removed from UITableView

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

enter image description here 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?

like image 818
Steve Mclean Avatar asked Oct 14 '18 14:10

Steve Mclean


People also ask

How to use a custom cell in a UITableView?

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.

How to avoid cells bumping each other in uitableviewdelegate?

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{ ... }

What is the difference between UIViewController and static table view?

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.

How to populate data on the contactstableview using uitableviewdatasource?

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.


1 Answers

You need to lower priority of bottom constraint

let con = cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)  
con.priority = UILayoutPriority(999)
con.isActive = true
like image 73
Sh_Khan Avatar answered Oct 29 '22 14:10

Sh_Khan