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!
