Collectionview 动态高度变化与约束在 Swift 中不起作用 [英] Collectionview dynamic height change with constraints not working in Swift
问题描述
我在tableview单元格中使用collectionview,tableview单元格的层次结构是这样的
I am using collectionview in tableview cell and the hierarchy of tableview cell is like this
topview 约束 top = 0,leading = 0, trailing = 0,bottom =>0
topview constraints top = 0, leading = 0, trailing = 0, bottom => 0
ViewX 约束 top = 0 前导 = 0,尾部 = 0,底部 =>10
collectionviewViewX 约束 top =>10 到 ViewX,前导 = 0,尾随 = 0,底部 = 10
collectionviewViewX constraints top => 10 to ViewX, leading = 0, trailing = 0, bottom = 10
附件集合视图约束 top = 10 到标签,前导 = 0,尾随 = 0,底部 = 10,高度 = 50
Stackview 约束 top = 10,leading = 0, trailing = 0,bottom = 10,height = 50
Stackview constraints top = 10, leading = 0, trailing = 0, bottom = 10, height = 50
和tableview单元格代码:但是使用此代码,collectionview高度不会根据单元格数量而改变..如何根据来自JSON的单元格数量更改collectionview高度
and tableview cell code: but with this code collectionview height is not changing according to cells count.. how to change collectionview height according to number of cells coming from JSON
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 there are 10 cells.. but hear all cells are not showing.. how to show all cells please do help me.. i got stuck here from long
根据下面的答案o/p是这样的..如何将单元格移动到左侧..当前单元格在中间
according to below answer o/p coming like this.. how to move cell to left side.. currently cell in the middle
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
}
推荐答案
将此添加到您表的 cellForRow
并检查它是否有效:
Add this to your table's cellForRow
and check if it works:
cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.attetchmentsCollectionview.reloadData()
cell.attCollHeight.constant = cell.attetchmentsCollectionview.collectionViewLayout.collectionViewContentSize.height;
左对齐的单元格和每行 1 个项目,添加此委托方法:
Left aligned cell and 1 item per row, add this delegate method:
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 动态高度变化与约束在 Swift 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!