Any Idea, why am I not able to get the delete button when I divide the list into two columns and how can I get that in this problem. The delete button was working fine until when I divided the list into two columns. Can anyone please help me to implant the delete button on the list while having two columns. Here's the code I was working on.
import SwiftUI
import Swift
struct Calculation: View {
@State var load1 = Float()
@State var load2 = Float()
@State var gp : Float = 0
@State var rate: Float = 0
@ObservedObject var taskStore = TaskStore()
@StateObject private var model = Model()
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count + 1), toDoItem: " Earning: = \(rate.description)" ))
}
var body: some View {
NavigationView {
VStack {
List {
Section(header:Text("load 2"))
{
TextField("Enter value of load 1", value: $load1, format: .number)
TextField("Enter value of load 1", value: $load2, format: .number)
}
HStack {
Button(String(format: "Add Load"), action:
{
print("pay for the shift is ")
print(Rocky(mypay: rate))
gp += rate
})
Button(action: {addNewToDo(); Rocky(mypay: rate) }, label: {
Text(" ")
})
}
LazyVGrid(columns: model.columns, spacing: 32) {
ForEach(self.taskStore.tasks) {
task in
Text(task.toDoItem)
}
.onDelete(perform : self.delete) //For each
}
}.navigationBarTitle("SHIFTS")
.navigationBarItems(trailing: EditButton()) //List
Text("Gross Pay = $\(gp) ")
}.onAppear()
} }
func Rocky(mypay: Float)
{ rate = load1 + load2
print("Sus \(gp)")
}
func delete(at offsets : IndexSet) {
taskStore.tasks.remove(atOffsets: offsets)
gp -= rate
}
}
struct BlueTwoView_Previews: PreviewProvider {
static var previews: some View {
Calculation()
}
}
struct Task : Identifiable {
var id = String()
var toDoItem = String()
}
class TaskStore : ObservableObject {
@Published var tasks = [Task]()
}
struct GridData: Identifiable, Equatable {
let id: Int
}
class Model: ObservableObject {
@Published var data: [GridData]
let columns = [
//GridItem(.aspectRatio(1.0, contentMode: .fit))
GridItem(.adaptive(minimum: .infinity, maximum: .infinity)),
GridItem(.adaptive(minimum: .infinity, maximum: .infinity))
// GridItem(.fixed(200))
]
init() {
data = Array(repeating: GridData(id: 0), count: 100)
for i in 0..<data.count {
data[i] = GridData(id: i)
}
}
}
LazyVGridand implement it in the view in theForEach. You may find it easier to refactor that view out and implement you delete button in the view, and then use that in theForEach.