【问题标题】:Collectionview dynamic height change with constraints not working in SwiftCollectionview 动态高度变化,约束在 Swift 中不起作用
【发布时间】:2021-10-12 13:20:07
【问题描述】:

我在tableview单元格中使用collectionview,tableview单元格的层次结构是这样的

顶视图约束top = 0, leading = 0, trailing = 0, bottom => 0

ViewX 约束top = 0 leading = 0, trailing = 0, bottom => 10

collectionviewViewX 约束top => 10 to ViewX, leading = 0, trailing = 0, bottom = 10

附件集合视图约束top = 10 to label, leading = 0, trailing = 0, bottom = 10, height = 50

Stackview 约束top = 10, leading = 0, trailing = 0, bottom = 10, height = 50

和 tableview 单元格代码:但是使用此代码,collectionview 高度不会根据单元格计数而改变.. 如何根据来自 JSON 的单元格数更改 collectionview 高度

 class ProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var attCollHeight: NSLayoutConstraint!
@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
    
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
          layout.minimumInteritemSpacing = 0
          layout.minimumLineSpacing = 5
    let width = UIScreen.main.bounds.width/2.5
            let height = CGFloat(40)
            layout.itemSize = CGSize(width: width, height: height)
          self.attetchmentsCollectionview.collectionViewLayout = layout
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return attachArray?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AttatchmentCollectionViewCell", for: indexPath) as! AttatchmentCollectionViewCell

    var attatchBid = attachArray?[indexPath.item]

    self.attCollHeight.constant = collectionView.contentSize.height

    return cell
}

}

o/p 有 10 个单元格.. 但是听说所有单元格都没有显示.. 如何显示所有单元格请帮帮我.. 我卡在这里很久了

编辑:根据下面的答案 o/p 像这样..如何将单元格移动到左侧..当前单元格在中间

override func awakeFromNib() {
super.awakeFromNib()

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
      layout.minimumInteritemSpacing = 0
      layout.minimumLineSpacing = 0

let width = UIScreen.main.bounds.width/2.1
        let height = CGFloat(40)
        layout.itemSize = CGSize(width: width, height: height)
      self.attetchmentsCollectionview.collectionViewLayout = layout
 }

【问题讨论】:

    标签: swift uicollectionview height cell


    【解决方案1】:

    将此添加到您的表的cellForRow 并检查它是否有效:

      cell.frame = tableView.bounds
            cell.layoutIfNeeded()
            cell.attetchmentsCollectionview.reloadData()
            cell.attCollHeight.constant = cell.attetchmentsCollectionview.collectionViewLayout.collectionViewContentSize.height;
    

    编辑:

    左对齐单元格和每行 1 个项目,添加此委托方法:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: collectionView.frame.width - flowLayout.itemSize.width)
    }
    

    【讨论】:

    • 非常感谢您..我已经接受了您的回答..但可以在这里指导我..我在帖子末尾添加了我更新的代码..如何将collectionview单元格移动到左侧..目前在中间
    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 2019-04-07
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多