更新cellforRow中的约束时出现约束错误

时间:2018-04-16 07:21:53

标签: ios uitableview autolayout

我有一个带有collectionView的UITableViewCell。约束非常简单,前导0,前导0,前10和后10,UICollection视图的高度约束为160。将IBOutlet连接到高度约束。在行的单元格中,我正在更新高度约束。

但是在调试中遇到错误

行的单元格代码是

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)>

2 个答案:

答案 0 :(得分:0)

试试这个

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
}

还从“接口”构建器中删除collectionView高度约束。现在您不必在cellForItemAtIndexPath中设置约束。

答案 1 :(得分:0)

根据您的错误报告,我似乎试图在此SO question中解决此问题。如果冲突约束包含UIView-Encapsulated-Layout-Height,则表示在基于自动布局计算动态高度期间发生冲突。只需将其中一个垂直约束优先级降低到999 - 如果代码有效,那就没关系了,你就可以了。