article

Thursday, August 12, 2021

SwiftUI Simple Delete Rows from List

SwiftUI Simple Delete Rows from List
ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    @State private var recipes = ["Bistek Tagalog", "Sinigang Na Baboy", "Ginisang Munggo", "Pork Estofado", "Pata Tim"]

    var body: some View {
    
        NavigationView {
            List {
                ForEach(recipes, id: \.self) { recipe in
                    Text(recipe)
                }
                .onDelete(perform: self.deleteRow)
            }
            .navigationBarTitle(Text("Recipes"))
        }
    }
    private func deleteRow(at indexSet: IndexSet) {
        self.recipes.remove(atOffsets: indexSet)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post