Home > database >  SwiftUI : How I can delete row of ForEach in VStack?
SwiftUI : How I can delete row of ForEach in VStack?

Time:04-09

This is TaskModel

import Foundation
import SwiftUI


struct TaskModel: Identifiable, Codable {
    var id = UUID().uuidString
    var title : String
    var selectedColor : Color
    var remindedTime : Int
    var taskDate : Date
}

This is ViewModel I tried

    import Foundation
    import SwiftUI
    
    class CalendarViewModel : ObservableObject {
        
        @Published var tasks : [TaskModel] = []
        @Published var currentMonth : Int = 0
        
    
        func addTask(title : String, selectedColor : Color, reminderTime : Int, taskDate : Date) {
            let newTask = TaskModel(title: title, selectedColor: selectedColor, remindedTime: reminderTime * 60, taskDate: taskDate)
            tasks.append(newTask)
        }
        
        func deleteTask(task : TaskModel) {
            if let index = tasks.firstIndex(where: {$0.id == task.id }) {
                tasks.remove(at: index)
            }
        }

This is the ForEach I tried to delete row

VStack {
                    ForEach(vm.tasks.filter({vm.isSameDay(date1: $0.taskDate, date2: currentDate)})) { task in
                        TaskRowView(task: task)
                            .actionSheet(isPresented: $isShowActionSheet) {
                                ActionSheet(title: Text("Settings"), message: Text("Press the button that what you want to do            
  • Related