1

I have list items in SwiftUI, and when I delete list items I want to delete after alert menu, like

"do want to delete your list items, ""yes" or "no"

is it possible?

  struct MyView: View {

    @State private var selectedUsers: MyModel?
 
    var body: some View {
        
            ScrollView(.vertical, showsIndicators: false, content: {
                
                VStack(content: {
                    
                    ForEach(datas){ data in
  
                        MyRowView(data: data)
                         
                            .contextMenu {

                                Button(action: {
                                    self.delete(item: data)
                                }) {
                                    Text("delete") 
                                }
                               
                            }
                            .onTapGesture {
                                selectedUsers = data
                            
                            }
                        
                    } .onDelete { (indexSet) in
                       self.datas.remove(atOffsets: indexSet)
                    }})
           })}

    private func delete(item data: MyModel) {
               if let index = datas.firstIndex(where: { $0.id == data.id }) {
                   datas.remove(at: index)
               }
           }}
2

1 Answer 1

2

In the delete action you set a @State bool to true, this triggers e.g. a ConfirmationDialog – and only after confirming there, you really delete:

struct TestView: View {
    @State private var selectedUsers: MyModel?
    @State private var confirmDelete: Bool = false
    @State private var indexSet: IndexSet?
    
    @State var datas: [MyData] = []
    
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) {
            VStack {
                
                ForEach(datas){ data in
                    
                    MyRowView(data: data)
                        .contextMenu {
                            Button("Delete") {
                                delete(item: data)
                            }
                            
                        }
                        .onTapGesture {
                            selectedUsers = data
                        }
                    
                }
                .onDelete { indexSet in
                    indexSet = indexSet
                    confirmDelete = true
                }
                .confirmationDialog("Do you really want to delete?", isPresented: $confirmDelete) {
                    Button("Delete", role: .destructive) {
                        selectedUsers.remove(atOffsets: indexSet)
                    }
                    Button("Cancel", role: .cancel) { }
                }
            }
        }
        
    }
    
    private func delete(item data: IndexSet) {
        if let index = datas.firstIndex(where: { $0.id == data.id }) {
            datas.remove(at: index)
        }
    }
}

Sign up to request clarification or add additional context in comments.

7 Comments

tnx but I do not understand ur answer, what is @State var Bool?
in your MyView at the top declare a var: @State private var confirmDelete = false
do you want the delete confirmation for the button or for the list delete function?
it says Cannot find 'indexSet' in scope
Your code is somehow not complete. You are listing datas– what is that? – then select on tap, and want to delete from selectedUsersby swipe?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.