[英]how to hide particular cells in collectionview in swift
In my project i got a array which i am loading on my collectionview 在我的项目中,我得到了一个我正在我的collectionview上加载的数组
var dataSource = ["@", "@", "1", "2", "3", "4", "5", "6", "7", "8" , "9", "10", "@", "@"]
for the string "@" i want to hide that particular cell. 对于字符串“@”我想隐藏该特定单元格。 so initially i was trying to do it using indexpath and then tried to check if my array position got value "@". 所以最初我试图使用indexpath,然后尝试检查我的数组位置是否得到值“@”。 But i am unable to hide it properly as some other cell would get changed on scroll 但我无法正确隐藏它,因为其他一些单元格会在滚动时被更改
So this what i have done on my cellForItemAt : 这就是我在我的cellForItemAt上所做的 :
if dataSource[indexPath.row] == "@" {
cell.contentView.isHidden = true
cell.layer.borderColor = UIColor.white.cgColor
}
Things to be considered its scrolling horizontally and this is my sizeForItemAt : 事情被认为是水平滚动,这是我的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 )
}
You are reusing the cell, so you need to add else part also of that condition to set isHidden
to false
and default borderColor
. 您正在重用该单元格,因此您还需要添加该条件的其他部分以将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
}
Also if you doesn't want to show the cell that why don't you remove that element from your array using filter
. 此外,如果您不想显示单元格,为什么不使用filter
从阵列中删除该元素。
dataSource = dataSource.filter { $0 != "@" }
And now just reload the collectionView
. 现在只需重新加载collectionView
。
You can completely get rid of that cells only by filtering your dataSource array. 只有通过过滤dataSource数组,才能完全摆脱这些单元格。
var filtered = dataSource.filter { (item) -> Bool in
item != "@"
}
and use this filtered array instead of source. 并使用此筛选的数组而不是源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.