I'm creating a List that renders a nested array using OutlineGroup. Im trying to implement .onDelete to this ForEach loop.
.onDelete is not working properly on this ForEach loop.
It can delete top-level rows correctly, but it does not delete child-level rows correctly.
I think IndexSet is not correctly targeting focusExample[subFocus]. (it gives an error when I try)
How can I get .onDelete to work correctly in this OutlineGroup?
struct FocusArea: Identifiable {
let id = UUID()
let focus: String
let time: Int
let subFocus: [FocusArea]?
}
struct FocusView: View {
@State private var focusExample =
[ FocusArea(focus: "PhD", time: 20, subFocus: [
FocusArea(focus: "Experimenting", time: 15, subFocus: nil),
FocusArea(focus: "Writing", time: 5, subFocus: nil)
]),
FocusArea(focus: "Reading", time: 10, subFocus:[
FocusArea(focus: "Holes", time: 7, subFocus: nil),
FocusArea(focus: "Harry Potter", time: 3, subFocus: nil)
]),
FocusArea(focus: "Piano", time: 5, subFocus: nil)
]
func delete(at offset: IndexSet) {
focusExample.remove(atOffsets: offset)
}
var body: some View {
NavigationView{
List{
ForEach(focusExample) { focus in
OutlineGroup(focus, children: \.subFocus) { subFocus in
HStack {
Text(subFocus.focus)
Spacer()
Group {
Text("\(focus.time)")
.font(Font.system(.body, design: .rounded).weight(.medium))
+
Text(" HR")
.font(Font.system(.body, design: .rounded).weight(.medium).smallCaps())
}.foregroundColor(Color.green)
}
}
}.onDelete(perform: delete)
}
.toolbar {
EditButton()
}
.navigationTitle("Focus")
}
}
}