CollectionView 不显示最后一部分

CollectionView not displaying last section

我有一个自定义的 UICollectionView,它共有 4 个(0、1、2、3)部分。我正在做的是,如果没有信息显示 section 2,那么我将此部分的单元格大小设置为 CGSize(width: view.bounds.width, height: 0)。问题是第 3 节中的单元格也不会显示,甚至认为 CGSize(width: view.bounds.width, height: 50) 在我的 section 3.

开关中的 sizeForItemAtIndexPath 上被调用

到目前为止,我发现我在 cellForItemAtIndexPathcase 2 and 3 中的 switch 在这种情况下没有被调用。

我还发现将 section 2 设置为 return 0 单元格显示 section 3.

我想我缺少的是 layout 中我做错的东西......也许你们中的一个人以前遇到过这个。

这是我的一些代码。

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    switch indexPath.section {
    case 0: return CGSize(width: view.bounds.width, height: 50)
    case 1: return CGSize(width: view.bounds.width, height: 75)
    case 2:
        if let description = description , description.characters.count > 0 {
            let sizingNibNew = NSBundle.mainBundle().loadNibNamed("BioCollectionViewCell", owner: BioCollectionViewCell.self, options: nil)
            let sizingCellNew = sizingNibNew?[0] as! BioCollectionViewCell

                sizingCellNew.myText = description

            sizingCellNew.initialize()

            return CGSize(width: view.bounds.width, height: sizingCellNew.myTextView.frame.height + 10)
        } else {
            return CGSize(width: view.bounds.width, height: 0)
        }
    case 3:
        return CGSize(width: view.bounds.width, height: 50)
    default:
        return CGSize(width: view.bounds.width, height: 0)
    }
}


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 4
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    switch section {
    case 0: return 4
    case 1: return 1
    case 2: return 1
    case 3: return 2
    default:
        return 0
    }
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    switch indexPath.section {

    case 0:
        switch indexPath.item {
        case 0: 
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MapView", forIndexPath: indexPath) as! MapView

            if let address = address, let city = city {
                cell.setAddressInMapView(address, cityAddress: city)
            }

            return cell
        case 1: 
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TitleCell", forIndexPath: indexPath) as! TitleCell

            cell.titleLabel.text = title1

            return cell

        case 2: 
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell

            cell.nameLabel.text = xName

            return cell
        case 3: // address
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AddressCell", forIndexPath: indexPath) as! AddressCell

            cell.addressLabel.text = address

            return cell
        default:
            return UICollectionViewCell()
        }

    case 1:
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Name", forIndexPath: indexPath) as! ShowFeaturedArtist

        cell.xId = id ?? ""
        cell.nameLabel.text = name ?? ""

        cell.populateViewWithoutQuery()

        return cell

    case 2:
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BioCollectionViewCell", forIndexPath: indexPath) as! BioCollectionViewCell

        if let description = description where description.characters.count > 0 {
            cell.myText = description
        }
        cell.initialize()

        return cell

    case 3:
        switch indexPath.item {
        case 0:
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserAttending", forIndexPath: indexPath) as! UserAttending

            cell.label.text = "0 " + " Users Attending"
            return cell

        case 1:
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserAttending", forIndexPath: indexPath) as! UserAttending

            cell.label.text = "Release Dates"
            return cell

        default:
            return UICollectionViewCell()

        }

    default:
        return UICollectionViewCell()
    }
}

我不想显示一个部分,int numberOfItemsInSection 方法,对于你的判别式(没有要显示的项目),该部分应该 return 0(零),并且 cellForItemAtIndexPath 永远不会要求该部分。

改变单元格高度的方法不对

你为什么不把你的numberOfSectionsInCollectionView方法改成return 4,只有当第二部分有数据要显示时,否则改成return 3

这样您就不需要更改单元格的高度,无论如何这都不是一个好方法。

通过在 numberOfItemsInSection 中执行此操作修复:

switch section {
    case 0: return 6
    case 1: return 1
    case 2:
        if show._description?.characters.count > 0 {
            return 1
        } else {
            return 0
        }
    case 3: return 2