首页 文章

集合视图第一个单元大小问题

提问于
浏览
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中的第0个索引处的单元格,隐藏了1个部分,但仍然显示 .
enter image description here

2 回答

  • 1

    您不应该通过尝试将大小设置为0来执行此操作 . 如果将其设置为0,则基础代码读取为尝试自动调整大小(这就是为什么将其设置为接近0的小值看起来几乎就是这样作品) . 您想要实际做的只是调整您的数据源 . 因此,当它要求项目数量返回没有您想要隐藏的项目的金额时,cellForItem应该只考虑该项目不在那里 .

  • 0

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

相关问题