Search code examples
swiftbuttoncore-datadelete-row

Swiftui coredata foreach scrollview : delete button


I'm trying to add delete button on each list. (not using swipe function)

This is my current summary code.

import SwiftUI
struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Saving.entity(), sortDescriptors: [
        NSSortDescriptor(KeyPath: \Saving.orders, ascending: true),
        ~~~
    ]
    ) var savings : FetchedResults<Saving>
    
    var body: some View {
        NavigationView{
            ScrollView(.vertical, showsIndicators: false){
                ForEach(savings, id : \.self) { save in
                    ZStack{
                        HStack(alignment: .top){
                            Menu(content: {Button(action: {
                                                    self.delete(at:IndexSet.init(arrayLiteral: 0))}) {
                                Text("Delete")
                                Image(systemName: "trash")}
                            })  {
                                Image(systemName:"line.horizontal.3")}
                        }
                    }
                }
            }
        }
    }
    func delete(at offsets: IndexSet) {
        for index in offsets {
            let deletes = saving[index]
            self.moc.delete(deletes)
        }
        try! self.moc.save()
    }
}

I can designate the index number at button action and when i tap the button in the list which has a lat of rows, i can delete only the first row again and again. It just like when i tap the delete button in any row, the first row is always deleted. it's obvious cause the index number is 0. I want to delete the row when i tap the button in that row. How can i fix this problem? Thank you guys!


Solution

  • You need to pass correct index insted of ```IndexSet.init(arrayLiteral: 0)``

    So use indices and first find the index and pass as an Int index. Like this,

    Body view

    var body: some View {
            NavigationView{
                ScrollView(.vertical, showsIndicators: false){
                    ForEach(savings.indices, id : \.self) { index in //<--- Here
                        let save = savings[index] //<--- Here
                        ZStack{
                            HStack(alignment: .top){
                                Menu(content: {Button(action: {
                                                        self.delete(at: index)}) { //<--- Here
                                    Text("Delete")
                                    Image(systemName: "trash")}
                                })  {
                                    Image(systemName:"line.horizontal.3")}
                            }
                        }
                    }
                }
            }
        }
    

    Delete function

    func delete(at index: Int) { //<--- Here
            let deletes = saving[index]
            self.moc.delete(deletes)
        try! self.moc.save()
    }