简体   繁体   English

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

[英]Collection view first cell size issue

i have to hide some of the cells of uicollectionview based on my api response. 我必须根据我的api响应隐藏uicollectionview的某些单元格。

i am setting cell size to zero in collectionview's sizeForItemAtIndexPath method based on my json response value. 我根据我的json响应值在collectionview的sizeForItemAtIndexPath方法中将单元格大小设置为零。

but even after setting the size to zero the cell which is at 0th index is always visible. 但是即使将大小设置为零,位于第0个索引的单元格始终可见。

i can not filter out and remove the data from the array. 我无法过滤掉并从阵列中删除数据。 i need that data for some operations. 我需要一些操作的数据。

my UICollectionView datasource method. 我的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
}

my flow layout delegate method. 我的流程布局委托方法。

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)
    }

expected result is the cell at 0th index in 0 and 1 section is hidden but it is still displayed. 预期的结果是隐藏了0和1部分中第0个索引的单元格,但仍将其显示。 在此处输入图片说明

You shouldn't do this by trying to set the size to 0. If you set it to 0 underlying code reads that as trying to automatically size it for you (this is why setting it to a small value close to 0 looks like it almost works). 您不应尝试通过将大小设置为0来执行此操作。如果将其设置为0,则底层代码会将其读为试图为您自动调整大小(这就是为什么将其设置为接近 0的小值看起来几乎就像它一样的原因。作品)。 What you want to actually do is just adjust your data source. 您实际要做的只是调整数据源。 So when it asks for the number of items return the amount without the items that you want hidden, and cellForItem should just take into account that the item isn't there. 因此,当它要求提供物品数量时,返回的数量将不包含您要隐藏的物品,而cellForItem应该仅考虑到物品不存在的情况。

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

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

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