如何过滤collectionview单元格?



我有一个collectionview单元格,需要过滤单元格。

我试图使单元格高度为0。但不工作。

这是我的代码。

扩展ViewController: UICollectionViewDataSource, UICollectionViewDelegate{

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return self.numberOfCell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? data else {
return UICollectionViewCell()
}

let set1 = CoreDataManager.shared.getSetting(idx: indexPath.row).set1

if(set1){

return cell
}
else{
let none = collectionView.dequeueReusableCell(withReuseIdentifier: "none", for: indexPath)
none.frame.size.width =  100
none.frame.size.height =  CGSize(width: 0, height: 100).width
return none
}
}

}

如果你需要设置一个单元格的大小,你需要符合UICollectionViewDelegateFlowLayout和实现sizeForItemAt并给它的大小。

extension viewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 0)}
}

另一个解决方案,使高度约束在您的单元格内的视图与标识符"cell"并在cellForItemAt中返回单元格之前将其赋值为0。

最新更新