如何在UICollectionView中显示多个部分

Chr*_*sen 5 uicollectionview uicollectionviewcell swift swift3

集合视图仅显示第一部分,但不显示第二部分.

这是代表们

private func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    var returnValue = Int()

    if section == 0 {
        returnValue = 1

    } else if section == 1 {
        returnValue = self.section1DataSource.count

    }

    return returnValue

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = UICollectionViewCell()

    let cellA:GroupPhoto = collectionView.dequeueReusableCell(withReuseIdentifier: "groupPhotoCard", for: indexPath) as! GroupPhoto
    let cellB:AddFriendCell = collectionView.dequeueReusableCell(withReuseIdentifier: "addFriendCard", for: indexPath) as! AddFriendCell

    let section = indexPath.section

    if section == 0 {

        cell = cellA

    //end of section 0
    } else if section == 1 {
        cell = cellB
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    var returnValue = CGSize()

    if indexPath.section == 0 {

        returnValue = CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height / 3.0 - 8)

    } else if indexPath.section == 1 {
        returnValue = CGSize(width: collectionView.frame.size.width / 2.9 - 8, height: collectionView.frame.size.height / 2.9 - 8)
    }

    return returnValue
}
Run Code Online (Sandbox Code Playgroud)

当我交换条件时,第一部分的单元格将被分配给第二部分.它工作正常,但它仍然只显示第一部分而不是第二部分.

Nir*_*v D 10

numberOfSections(in:)在Swift 3中更改了签名,它应该是这样的.

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 2 
}
Run Code Online (Sandbox Code Playgroud)