【发布时间】:2021-09-02 21:53:57
【问题描述】:
我想根据每个单元格内最高的UILabel 设置我的UICollectionViewCell 和UICollectionView 的高度。
为此,这是我的尝试:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewLayout: UICollectionViewFlowLayout!
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
let datasource = ["USA", "MGA USA USA USA USA", "FRA", "GER", "MGA", "FRA", "GER", "MGA", "FRA", "GER", "MGA", "FRA", "GER USA USA USA USA USA USA", "MGA", "FRA", "GER", "MGA", "GER USA USA USA USA USA USA GER USA USA USA USA USA USA GER USA USA USA USA USA USA GER USA USA USA USA USA USA", "GER",]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self;
collectionView.delegate = self;
let nib = UINib.init(nibName: "CollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
collectionViewLayout.minimumInteritemSpacing = 10000.0;
}
// override func viewDidLayoutSubviews() {
// super.viewDidLayoutSubviews()
// let height = collectionView.collectionViewLayout.collectionViewContentSize.height
// collectionViewHeight.constant = height
// self.view.layoutIfNeeded()
// }
var maxHeight = 0
var indexPath: IndexPath?
func newMax(){
collectionViewHeight.constant = CGFloat(maxHeight+60)
collectionView.collectionViewLayout.invalidateLayout()
if let index = indexPath {
collectionView.reloadItems(at: [index])
// collectionView.reloadSections(IndexSet(integer: 0))
}
print("collectionViewHeight.constant \(collectionViewHeight.constant)")
}
func checkMax(height: Int, forItemAt indexPath: IndexPath ){
var newMax = height > maxHeight ? height: maxHeight
if(newMax != maxHeight) {
maxHeight = newMax
self.newMax()
self.indexPath = indexPath
print("max is \(maxHeight)")
}
}
override func viewDidAppear(_ animated: Bool) {
guard let cells = collectionView.visibleCells as? Array<CollectionViewCell> else {
return
}
for cell in cells {
let height = cell.countryNameLabel.bounds.size.height
print(cell.countryNameLabel.text)
print(height)
checkMax(height: Int(height), forItemAt: IndexPath(index: 0))
}
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? CollectionViewCell {
let height = cell.countryNameLabel.bounds.size.height
print(cell.countryNameLabel.text)
print(height)
checkMax(height: Int(height), forItemAt: indexPath)
} else {
print(cell.bounds.size.height)
}
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
// let h = maxHeight == 0 ? 123: maxHeight
let h = collectionViewHeight.constant - 30
print("setting cell size to \(h)")
return CGSize(width: 123, height: h)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
datasource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellTemp = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? CollectionViewCell
print("cellTemp \(cellTemp)")
let cell = cellTemp ?? CollectionViewCell()
cell.setup(with: datasource[indexPath.row])
return cell
}
}
这是输出:
正如您在此 GIF 中看到的那样,我无法理解为什么 UICollectionViewCell 会在 scroll 上随机消失。我尝试了很多在 StackOverflow 上找到的东西,但我不走运。这就是我在这里问这个问题的原因。
如果我的整个方法是错误的并且有更好的方法,请提出建议。我也尝试了很多我在这里找到的东西,但这是唯一几乎可行的方法
【问题讨论】:
标签: ios swift uitableview uicollectionview uicollectionviewcell