I am creating an app with a contacts like view. I'm using Core Data, and I have a "company" entity, and a "contact" entitiy. There is a one-to-many relationship, where a company can have many contacts, but a contact can only have one company.
I'd like to create a list of companies, with the ability to "expand" the list to show all the contacts in that company in-line.
I first looked at using the built-in children property of list, but that requires the objects be of the same type.
Now I'm trying to manually create this using a disclosure group.
When I include the disclosure group as part of the list, it seems to work with individual views:
import SwiftUI
struct CompaniesView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: []) var companies: FetchedResults<Company>
var body: some View {
NavigationView {
List {
ForEach(companies, id: \.self) { company in
CompanyRow(company: company)
}
}
}
}
}
struct CompanyRow: View {
@ObservedObject var company: Company
@Environment(\.managedObjectContext) var moc
var body: some View {
VStack(alignment: .leading) {
Text("\(company.wrappedName)")
.font(.headline)
DisclosureGroup("Show contacts") {
Text("Person A")
Text("Person B")
Text("Person C")
}
}
}
}
Screen Shot 1
However, I'd like each item to show up as it's own row in the list, so I've tried putting in another list around the contact elements, but the view doesn't grow to show each item.
import SwiftUI
struct CompaniesView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: []) var companies: FetchedResults<Company>
var body: some View {
NavigationView {
List {
ForEach(companies, id: \.self) { company in
CompanyRow(company: company)
}
}
}
}
}
struct CompanyRow: View {
@ObservedObject var company: Company
@Environment(\.managedObjectContext) var moc
var body: some View {
VStack(alignment: .leading) {
Text("\(company.wrappedName)")
.font(.headline)
DisclosureGroup("Show contacts") {
List {
Text("Person A")
Text("Person B")
Text("Person C")
}
}
}
}
}
Screen Shot 2
I'm not sure if there is a way to make this work or if I should be looking for an entirely new solution, but any advice is apprecaited!