Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display multiple sections in UICollectionView

Collection view only shows the first section, but doesn't show the second section.

Here are the delegates

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
}

When I interchange the conditions so that the first section's cell will be assigned to the second section. It works fine, but it will still only show the first section and not the second.

like image 311
Chris Mikkelsen Avatar asked Jan 30 '17 06:01

Chris Mikkelsen


People also ask

How do I have more than one type of cell in a collection view?

Just like you would for a single cell. Dragging multiple cells into your collection view in a Storyboard automatically registers these cells on your collection view.

How do I add a section in collection view?

Select the starter app project's Views folder and click Create to create the file. Next, open Main. storyboard and select the collection view in FlickrPhotosViewController . Select the Attributes inspector and, in the Accessories section, check the Section Header checkbox.

What is the difference between Uitableview and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .

What is UICollectionView flow layout?

Overview. A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.


2 Answers

Signature of numberOfSections(in:) is changed in Swift 3 it should be like this.

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 2 
}
like image 131
Nirav D Avatar answered Nov 03 '22 01:11

Nirav D


In swift 3, numberOfSectionsInCollectionView is changed to numberOfSections(in:)

To avoid this, you should add UICollectionViewDataSource, to implement autocomplete.

like image 28
Carmelito R. Bayarcal Jr. Avatar answered Nov 03 '22 00:11

Carmelito R. Bayarcal Jr.