A simple Delete multiple rows The selected items will be removed in the live preview.
ContentView.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | // // ContentView.swift // swiftuidev // // Created by Cairocoders // import SwiftUI struct ContentView: View { @State var numbers = [ "Record1" , "Record2" , "Record3" , "Record4" , "Record5" , "Record6" , "Record1Seven" , "Record1Eight" , "Record1Nine" , "Record1Ten" ] @State var editMode = EditMode.inactive @State var selection = Set<String>() var body: some View { NavigationView { List(selection: $selection) { ForEach(numbers, id: \.self) { number in Text(number) } } .navigationBarItems(leading: deleteButton, trailing: editButton) .environment(\.editMode, self.$editMode) } } private var editButton: some View { if editMode == .inactive { return Button(action: { self.editMode = .active self.selection = Set<String>() }) { Text( "Edit" ) } } else { return Button(action: { self.editMode = .inactive self.selection = Set<String>() }) { Text( "Done" ) } } } private var deleteButton: some View { if editMode == .inactive { return Button(action: {}) { Image(systemName: "" ) } } else { return Button(action: deleteNumbers) { Image(systemName: "trash" ) } } } private func deleteNumbers() { for id in selection { if let index = numbers.lastIndex(where: { $0 == id }) { numbers. remove (at: index) } } selection = Set<String>() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |