0

I have a UITableViewCell with collectionView inside it. constraint are pretty simple leading 0 , trailing 0 , top 10 and bottom 10 and height constraint of UICollection view is 160 . Connected an IBOutlet to height constraint. In cell for row I am updating the heigth constraint.

But getting error in debug

Code of cell for row is

func videoCell(for tableView:UITableView ,with indexPath:IndexPath , videos:[VideoCollectionViewCellConfigure] ) -> VideoTableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "VideoTableViewCell", for: indexPath) as! VideoTableViewCell
    cell.videoConfigurators = videos
    cell.collectionCellClickedBlock = self.videoCollectionCellClicked(_:_:)

    if videos.count > 0 {
        let video = videos[0]
        cell.videoCollectionViewHeightConstraint.constant = video.height
    }

    cell.videoCollectionView.reloadData()
    return cell
}




    2018-04-16 12:42:10.815998+0530 CineBee[5280:74417] [LayoutConstraints] 
    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:0x6000006878a0 UICollectionView:0x7f86109c4c00.height 
   == 180   (active)>",
"<NSLayoutConstraint:0x604000885870 V:[UICollectionView:0x7f86109c4c00]-(10)-|   (active, names: '|':UITableViewCellContentView:0x7f8615803ca0 )>",
"<NSLayoutConstraint:0x604000888de0 V:|-(10)- 
   [UICollectionView:0x7f86109c4c00]   (active, names: '|':UITableViewCellContentView:0x7f8615803ca0 )>",
"<NSLayoutConstraint:0x60400088dde0 'UIView-Encapsulated-Layout-Height' 
    UITableViewCellContentView:0x7f8615803ca0.height == 180.5   (active)>"
 )

  Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x6000006878a0 
  UICollectionView:0x7f86109c4c00.height == 180   (active)>
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37

2 Answers2

0

Try this

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var defaultHeight = /*your default height*/
    if videos.count > 0 {
        let video = videos[0]
        return video.height + 10 + 10 // video height + top padding + bottom padding 
    }

    return defaultHeight
}

Also remove the collectionView height constraint from the Interface builder. Now you don't have to set constraint in the cellForItemAtIndexPath.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
0

According to your error report, it seems like the case I was attempting to address in this SO question. If the conflicting constraints contain UIView-Encapsulated-Layout-Height it means the conflict arises during calculating dynamic height based on autolayout. Just lower one of the vertical constraints priority to 999 - if the code works, it's ok and you are good to go.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90