我有UICollectionview
,列数随机变化。每次列数更改时,我都会调用以下函数。
func setTheCollectionViewSize(){
if self.activeLockerList.count > 0 {
self.btnLockerDropdown.setTitle("Lockers Set \(self.activeLockerList[0].lockerId)", for: UIControlState.normal)
}
screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
//if the collection view layout is not invalidated then the app will crash
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
//setting the collectionview size so there will be no white lines in the drawn boxes
var appropriateScreenWidth = Int(self.screenWidth)
while appropriateScreenWidth % Int(self.numberOfLockersPerColumn) != 0 {
appropriateScreenWidth = appropriateScreenWidth - 1
}
let itemWidth : CGFloat = CGFloat(appropriateScreenWidth) / self.numberOfLockersPerColumn
collectionViewWidthLocal.constant = CGFloat(appropriateScreenWidth)
layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
print(self.numberOfLockersPerColumn)
print(screenWidth)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 10.0, left: 0, bottom: 10.0, right: 0)
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.setCollectionViewLayout(layout, animated: false)
}
当每行的细胞计数增加时,它可以正常工作。但是,当计数减少应用程序在self.collectionView.setCollectionViewLayout(layout, animated: false)
正如您所看到的,使用self.collectionView.collectionViewLayout.invalidateLayout()
使收藏视图无效也无济于事。我一直花费数小时仍然没有运气。提前谢谢。
答案 0 :(得分:2)
您是否在setTheCollectionViewSize()
之前或之后调用collectionView.reloadData()
函数?
应按以下顺序进行。试着让我知道。
self.collectionView.reloadData()
self.setTheCollectionViewSize()
换句话说,以下应该是使collectionview无效时的顺序
self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout()
希望它可以帮助你。