0

我必须根据我的 api 响应隐藏 uicollectionview 的一些单元格。

我根据我的 json 响应值在 collectionview 的 sizeForItemAtIndexPath 方法中将单元格大小设置为零。

但即使将大小设置为零后,位于第 0 个索引的单元格始终可见。

我无法过滤掉并从数组中删除数据。我需要这些数据进行一些操作。

我的 UICollectionView 数据源方法。

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 5
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! testCell
    cell.lblTest.text = "\(indexPath.item)"
    return cell
}

我的流程布局委托方法。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if (indexPath.section == 0 && indexPath.item == 0) || (indexPath.section == 1 && indexPath.item == 0){
            return CGSize(width: 0, height: 0)
        }
        return CGSize(width: 50, height: 50)
    }

预期结果是 0 和 1 部分中第 0 个索引处的单元格被隐藏但仍显示。 在此处输入图像描述

4

2 回答 2

1

您不应该通过尝试将大小设置为 0 来执行此操作。如果将其设置为 0,则底层代码会将其读取为尝试为您自动调整大小(这就是为什么将其设置为接近0 的小值看起来几乎作品)。您实际上想要做的只是调整您的数据源。因此,当它询问项目数量时,返回不包含您想要隐藏的项目的金额,并且 cellForItem 应该只考虑该项目不存在。

于 2018-08-27T15:34:33.993 回答
0

您必须将其设置为接近 0 的数字,例如 0.1、0.01。

于 2018-08-27T08:27:13.100 回答