2

Let's say I've got

struct Person: Identifiable {
  var id = UUID()
  var name: String
  var company: String
}

I also have an array of people, like so:

class PeopleList: ObservableObject {

  @Published var people = [
    Person(name: "Bob", company: "Apple"),
    Person(name: "Bill", company: "Microsoft"),
    Person(name: "Brenda", company: "Apple"),
    Person(name: "Lucas", company: "Microsoft"),
  ]

//Various delete and move methods

}

I'd now like to create a list with sections, where every person is grouped based on their company. I've gotten to the following, but this gives me grouped sections for each person, so 4 sections. I'd like to end up with 2 sections, one for Apple and one for Microsoft.

struct PeopleView: View {
   @ObservedObject var peopleList = PeopleList()

   var body: some View {
      NavigationView {
         List {
            ForEach(peopleList.people) { person in
               Section(header: Text(person.company)) {
                  Text(person.name)
               }
            }
         }
      .listStyle(GroupedListStyle())
      }
   }
}

I hope that makes sense! Thanks!

0

1 Answer 1

3

try this:

struct Person: Identifiable {
    var id = UUID()
    var name: String
    var company: String
}

class PeopleList: ObservableObject {

    @Published var people = [
        Person(name: "Bob", company: "Apple"),
        Person(name: "Bill", company: "Microsoft"),
        Person(name: "Brenda", company: "Apple"),
        Person(name: "Lucas", company: "Microsoft"),
    ]

    func getGroups() -> [String] {

        var groups : [String] = []

        for person in people {
            if !groups.contains(person.company) {
                groups.append(person.company)
            }
        }
        return groups
    }
}

struct  ContentView: View {
    @ObservedObject var peopleList = PeopleList()

    var body: some View {
        NavigationView {
            List () {
                ForEach (peopleList.getGroups(), id: \.self) { group in
                    Section(header: Text(group)) {
                        ForEach(self.peopleList.people.filter { $0.company == group }) { person in

                            Text(person.name)
                        }
                    }
                }
            }.listStyle(GroupedListStyle())
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enter image description here

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

3 Comments

That's great, thanks Chris. Terribly sorry about the formatting in this comment, I'm learning how to use StackOverflow. I'm finding some issues with my move and delete methods now, how would you combine that with getGroups? I’m calling .onDelete and onMove with these methods: func deleteListItem(whichElement: IndexSet) { people.remove(atOffsets: whichElement) } and the move method is: func moveListItem(whichElement: IndexSet, destination: Int) { people.move(fromOffsets: whichElement, toOffset: destination) }
please check my answer as ok if you are satisfied and ask a new question in a "new question" and not here so that everybody can see what happens and not getting confused. Thank you.
Of course, thank you! I marked your answer as ok and create a new question here: stackoverflow.com/questions/61579471/….

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.