Steve Mclean
Steve Mclean

Reputation: 149

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?

Upvotes: 8

Views: 3291

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to lower priority of bottom constraint

let con = cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)  
con.priority = UILayoutPriority(999)
con.isActive = true

Upvotes: 25

Related Questions