Hello there, I try to delete rows by onDelete(perform:) modifier in expandable list in swiftUI and embed EditButton() So user can change the order of items in the List,
here is my code for View :
and Data Model:
I know if I want to use onDelete(perform:) I should put my data into the ForEach so I do this and here is my problem I don't know how to solve it children: \.subMenuItems part:
here is my code for View :
Code Block language import SwiftUI struct ContentView: View { var body: some View { NavigationView { VStack { List(sampleMenuItems, children: \.subMenuItems) { item in HStack { Text(item.name) .font(.system(.title3, design: .rounded)) .bold() } } } .listStyle(InsetGroupedListStyle()) .navigationTitle("List") .navigationBarItems(trailing: Button(action: {}) { EditButton() } ) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
and Data Model:
Code Block language import Foundation struct MenuItem: Identifiable,Hashable { var id = UUID() var name: String var subMenuItems: [MenuItem]? } let sampleMenuItems = [ MenuItem(name: "iphone model", subMenuItems: iphoneModelItems), MenuItem(name: "ipad model", subMenuItems: ipadModelItems), ] // Sub-menu items for iphone let iphoneModelItems = [ MenuItem(name: "pro max"), MenuItem(name: "pro"), MenuItem(name: "se") ] // Sub-menu items for ipad let ipadModelItems = [ MenuItem(name: "pro"), MenuItem(name: "air"), ]
I know if I want to use onDelete(perform:) I should put my data into the ForEach so I do this and here is my problem I don't know how to solve it children: \.subMenuItems part:
Code Block language List { ForEach(sampleMenuItems, children: \.subMenuItems) { item in HStack { Text(item.name) .font(.system(.title3, design: .rounded)) .bold() } } } code-block