在我的项目中,我得到了一个我正在我的collectionview上加载的数组
var dataSource = ["@", "@", "1", "2", "3", "4", "5", "6", "7", "8" , "9", "10", "@", "@"]
Run Code Online (Sandbox Code Playgroud)
对于字符串"@"我想隐藏该特定单元格.所以最初我试图使用indexpath,然后尝试检查我的数组位置是否得到值"@".但我无法正确隐藏它,因为其他一些单元格会在滚动时被更改
这就是我在我的cellForItemAt上所做的:
if dataSource[indexPath.row] == "@" {
cell.contentView.isHidden = true
cell.layer.borderColor = UIColor.white.cgColor
}
Run Code Online (Sandbox Code Playgroud)
事情被认为是水平滚动,这是我的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 )
}
Run Code Online (Sandbox Code Playgroud)
您正在重用该单元格,因此您还需要添加该条件的其他部分以设置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
}
Run Code Online (Sandbox Code Playgroud)
此外,如果您不想显示单元格,那么为什么不从数组中删除该元素filter.
dataSource = dataSource.filter { $0 != "@" }
Run Code Online (Sandbox Code Playgroud)
现在只需重装collectionView.
只有通过过滤dataSource数组,才能完全摆脱这些单元格.
var filtered = dataSource.filter { (item) -> Bool in
item != "@"
}
Run Code Online (Sandbox Code Playgroud)
并使用此筛选的数组而不是源.
| 归档时间: |
|
| 查看次数: |
4407 次 |
| 最近记录: |