swiftuiswiftui-foreachswiftui-swipeactions

SwiftUI from .ondelete to .swipeAction


I have a ForEach with a dynamic array with an .ondelete. The code works as expected. Now I want to switch to .swipeAction because I want to implement no fullSwipe. .onDelete works with IndexSet, how can I implement this in a .swipeAction? How get I rid of this error: "Cannot find 'indexSet' in scope"

 List{
                    ForEach(0..<scan2Clip.saveList.count, id:\.self) { value in
                        if searchValue == "" {
                            if scan2Clip.saveList[value] != "" && searchValue == ""{
                                ShowLineView(value: value)
                            }
                        }
                        else  if scan2Clip.saveList[value] != "" && scan2Clip.saveList[value].localizedCaseInsensitiveContains(searchValue){
                            ShowLineView(value: value)
                        }
                    } .swipeActions(edge: .trailing) {
                        Button(role: .destructive, action: {
                            self.scan2Clip.saveList.remove(atOffsets: indexSet)
                        } ) {
                          Label("Delete", systemImage: "trash")
                        }
                      }
//                    .onDelete{ (indexSet) in
//                        self.scan2Clip.saveList.remove(atOffsets: indexSet)
//                    }
                   
                }
``

Solution

  • Just give it single index, like

    Button(role: .destructive, action: {
        self.scan2Clip.saveList.remove(at: value)   // << here !!
    } ) {
      Label("Delete", systemImage: "trash")
    }