【发布时间】: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