繁体   English   中英

如何在swift中隐藏collectionview中的特定单元格

[英]how to hide particular cells in collectionview in swift

在我的项目中,我得到了一个我正在我的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 )
    }

您正在重用该单元格,因此您还需要添加该条件的其他部分以将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

只有通过过滤dataSource数组,才能完全摆脱这些单元格。

    var filtered = dataSource.filter { (item) -> Bool in
       item != "@"
    }

并使用此筛选的数组而不是源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM