0

I have an array of IDs ["one", "two", "three"...]

These Ids are matched against a json I get from a URLSession and then displayed in a collectionview. At the moment it's just one simple collectionView with the feature to reorder it by long press and drag. But I need the possibilty to split it up in sections.

The best thing would be to make the sections dynamic, so that I can add as many sections as I want and then sort the cells into the sections. I get what I could do if I had an array for every section like

section1 = ["one", "two"]

section2 = ["three", "four"]

But I have no Idea how to realize this in my current setup.

3
  • Have a look at this tutorial on UICollectionView's by Raywenderlich. Commented Sep 5, 2017 at 9:02
  • This tutorial doesnt help very much. I'm familiar with a CollectionView but there are no sections added or data splitted up Commented Sep 5, 2017 at 9:11
  • myArray = [["one", "two", "three", "four"]] (one section). At some point myArray = [["one", "two"], [ "three", "four"]]. You retrieve cellForRowAt: myObject = myArray[indexPath.section][indexPath.row]. numberOfSection: myArray.count. numberOfItemsInSections: myArray[section].count, in pseudo code. Commented Sep 5, 2017 at 9:19

1 Answer 1

2

Don't do it. I'm sure you could figure out a way to keep the single array, but don't. The maintenance nightmare will haunt you.

Create a view of the data which is broken into an array of arrays.

struct MySection {
    let sectionId: String // or whatever section metadata you need.
    let data: [Data] // or whatever your real type is.
}

In the collection view controller

var dataView: [MySection]

All the data in 1 section

self.dataView = [MySection(sectionId: "all data", data: self.data)]

Grouping the data into different sections

self.dataView = self.data.reduce([]) { dataView, datum in
    var dataView = dataView // make a mutable data view

    let sectionId = self.determineSectionIdFromDatum(datum) // <-- slotting into sections

    // Add into an existing section or into a new section
    if let index = dataView.index(where: { $0.sectionId == sectionId }) {
        let data = dataView[index].data + [datum]
        dataView[index] = MySection(sectionId: sectionId, data: data)
    } else {
        let data = [datum]
        dataView.append(MySection(sectionId: sectionId, data: data))
    }

    return dataView
}

At this point you will likely need to sort either the sections or the data within the sections.

Sign up to request clarification or add additional context in comments.

3 Comments

That reduce function is fancy. +1
@Jeffery Thomas thanks for your suggestion. I think I have a bit of reading to do until I fully understand everything ;-) First Problem I see is that my Array is at the moment stored in the UserDefaults, and this won't work with a Struct so I'll have to find a way around this
@theslash The type of self.data doesn't matter here. I used [Data] as a placeholder. It could be [String], [[AnyHashable: Any]], or whatever data type is returned by UserDefaults.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.