Created a UITableViewController table. It creates a certain number of cells. They are filled from the array. Custom cells. Every cell has a UIView. In the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) function tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) I add a constraint to this UIView that sets the height. If picsCount is 0, then a constraint is created with a height of 0, and if it is greater than zero, then with its own special height.

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! CustomPostCellTableViewCell let picsCount = allposts[indexPath.row].pic.count print(picsCount) if picsCount > 0 { let heightConstraint = NSLayoutConstraint(item: cell.viewVW, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100) cell.viewVW.addConstraint(heightConstraint) } else if picsCount == 0 { let heightConstraint = NSLayoutConstraint(item: cell.viewVW, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 0) cell.viewVW.addConstraint(heightConstraint) } return cell } 

When I start to scroll through this list, it turns out that some cells have zero height, others have not zero. And the first cells should have gone zero, but this is not observed, they are in discord.

And also in debug it gives out this:

 2016-09-01 23:37:18.723 FriendDime[10039:2879999] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x7a78c9d0 V:[UIView:0x7a7a2890(100)]>", "<NSLayoutConstraint:0x7a69c730 V:[UIView:0x7a7a2890(0)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7a78c9d0 V:[UIView:0x7a7a2890(100)]> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

I assume that the cell is reused and the constraint is being added to it again. And it turns out that in some cells it turns out two constraints, one with a zero height, and the second with a height of 100.

And I also want to add to this UIView dynamically images, the number of which will be different in each cell. And what does this work with pictures, too, will be such problems?

Explain how to deal with this?

  • You yourself are asking the system to use the newly available cells via dequeueReusableCellWithIdentifier. do removeConstraint if you want to replace it with another - Max Mikheyenko
  • Is it possible to use something else instead of dequeueReusableCellWithIdentifier? - cheerful_weasel

1 answer 1

Yes, the TableView (like the CollectionView) uses the same cells to save resources when scrolling. You can add @property c constraint's in the heir of the cell class so that in the cellForRowAtIndexPath method cellForRowAtIndexPath do not add a new one with each scrolling, but modify and initialize one single one.

You can also see the prepareForReuse method, you need to add it to the cell class, call removeConstraint in it.

ps to change the height of certain cells, you can use the tableview heightforrowatindexpath

UPD:

removeConstraint will remove with a single view the one passed as the argument. You can also use removeConstraints - an array of the attributes that you want to remove with a view is passed as an argument. In swift, you can do something like this:

 class TableViewCell: UITableViewCell { override func prepareForReuse() { super.prepareForReuse() for heightConstraint in self.viewVW.constraints { if heightConstraint.firstAttribute == .Height { self.viewVW.removeConstraint(heightConstraint) } } } } 
  • As for the tableview heightforrowatindexpath, everything is much more complicated for me. In addition to the UIView, I have other elements in the cell. There is a View Image for the avatar, set in the interface of the builder, labels for the user name and content, and all this with the controls. The height of the cell changes depending on the size of the contents. Therefore, I had to use the constraints for UIView, since we need to set it to strict height. - cheerful_weasel
  • Another question about removeConstraint. If I apply this to UIView, will it remove all the constraints that were added there? And I also want to add pictures from the code to this UIView. For example, in one cell there will be 3 pictures, in the other 1. Can they also be deleted as well? - cheerful_weasel
  • @cheerful_weasel added a small example. To remove pictures and generally any screen view, use the removeFromSuperview method. - Nerevarys
  • Unfortunately it did not work. All the same, the same. Still in debug for some cells, the message pops up that two constraints with different heights are used at the same time. And where to use removeFromSuperview? Well, in terms of what part of the code. - cheerful_weasel
  • mm, and the method comes in? - Nerevarys