Collectionview 动态高度变化,约束在 Swift 中不起作用
Collectionview dynamic height change with constraints not working in Swift
我在 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
和表格视图单元格代码:但使用此代码,collectionview 高度不会根据单元格计数而改变。如何根据来自 [=53 的单元格数量更改 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
}
将此添加到您 table 的 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)
}
我在 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
和表格视图单元格代码:但使用此代码,collectionview 高度不会根据单元格计数而改变。如何根据来自 [=53 的单元格数量更改 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
}
将此添加到您 table 的 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)
}