Curnelious
Curnelious

Reputation: 1

UICollectionView group sections?

Forgive me if its too basic, but I may miss something here.

I have a collection in which each section has 1 or more elements . What happens is that I see the whole list as 1 list with no differentiation between sections .

The effect I would like to get is natural, where if a section has 3 elements, I see only the first, and if I push it, I can see the other elements inside the section, push again to close.

Is it something I have to manually do? Its not the way it works with section now.

(whats the point of sections in this form? just to get the header? this I could do by my own)

Upvotes: 1

Views: 2296

Answers (2)

Curnelious
Curnelious

Reputation: 1

I found a way to do this pretty simply .

I do not use sections right away . What I do , is to set 1 section and multiple rows, then each row will show only the first element in the data array (assuming each element of data array is an array ). On selection I reload table with sections.

When user select a row , i check if it has multiple values :

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var inSection=[String]()
        inSection=data[indexPath.row]
    if ( inSection.count>1)
    {
        //MULTIPLE VALUES IN CELL
           OpenGroup=indexPath.row
           collectionView.reloadData()


    }

Also, when i load , I set delegates like this :

  func numberOfSections(in collectionView: UICollectionView) -> Int {
        if(OpenGroup > -1 ){ return data.count} //case open group

           return 1   //case closed group

    }


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

        if(OpenGroup > -1 ) {return data[section].count}
        return data.count
    }

In this case, when I have multiple values on selected cell, I just update a variable that say what section is open, and reload the collection .

As you see in the delegate, if there is an open cell, it will set the collection to be multiple sections, otherwise, 1 section and multiple rows.

It works perfectly and require very little code.

Data, stay as it was- with the grouping- so when user reload the table, he get the original data array .

Upvotes: 1

creeperspeak
creeperspeak

Reputation: 5521

There's no built-in way to have the sections expandable/collapsible the way you're describing, but you can totally do it if you really want to. Just set the size of the section to be big enough only to show one cell by default, and if somebody taps the section set the size of the section to be big enough to show the rest. You'll end up having to maintain an array of expanded sections so that each time you reload you know which sections to show one cell and which to show all.

It's a pain, but I've been asked to do it at jobs before, and it works just fine. There are some inevitable consequences to doing this kind of thing, but you can work through them if you really need to.

Upvotes: 2

Related Questions