gpt4 book ai didi

ios - 如何快速隐藏collectionview中的特定单元格

转载 作者:搜寻专家 更新时间:2023-10-30 22:29:27 24 4
gpt4 key购买 nike

在我的项目中,我有一个数组,我正在将其加载到我的collectionview

var dataSource = ["@", "@", "1", "2", "3", "4", "5", "6", "7", "8" , "9", "10", "@", "@"]

对于字符串“@”,我想隐藏那个特定的单元格。所以最初我尝试使用 indexpath 来做,然后尝试检查我的数组位置是否有值“@”。但我无法正确隐藏它,因为其他一些单元格会在滚动时发生变化

这就是我在我的 cellForItemAt 上所做的:

if dataSource[indexPath.row] == "@" {

cell.contentView.isHidden = true
cell.layer.borderColor = UIColor.white.cgColor

}

要考虑的事情是水平滚动,这是我的 sizeForItemAt :

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {


return CGSize(width: (self.numberCollectionView?.bounds.size.width)!/5 - 3, height: (self.numberCollectionView?.bounds.size.width)!/5 - 3 )
}

最佳答案

您正在重复使用该单元格,因此您还需要添加该条件的 else 部分以将 isHidden 设置为 false 和默认的 borderColor

if dataSource[indexPath.row] == "@" {

cell.contentView.isHidden = true
cell.layer.borderColor = UIColor.white.cgColor
}
else {
cell.contentView.isHidden = false
cell.layer.borderColor = UIColor.black.cgColor //Set Default color here
}

此外,如果您不想显示单元格,为什么不使用 filter 从数组中删除该元素。

dataSource = dataSource.filter { $0 != "@" }

现在只需重新加载 collectionView

关于ios - 如何快速隐藏collectionview中的特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com