0

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)
        }
    }
}
Ricky Chhetri
  • 89
  • 1
  • 7
  • [See this question](https://stackoverflow.com/q/66352196/7129318). – Yrb Apr 19 '22 at 15:26
  • Hi @Yrb , I tried to implent that example in my case, however, my case is the list items are stacked horizontally into two columns. I want the delete button to appear on each item when I press the edit button. Is there anyway you can help me with that. – Ricky Chhetri Apr 19 '22 at 15:49
  • @Yrb or may be dividing the list into two columns without grid layout and implementing the delete function. Any way to delete items when displayed in two columns?? – Ricky Chhetri Apr 20 '22 at 00:43
  • You need to post a [Minimal Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example) without extra code that shows what you have attempted. Your best bet is to use the `LazyVGrid` and implement it in the view in the `ForEach`. You may find it easier to refactor that view out and implement you delete button in the view, and then use that in the `ForEach`. – Yrb Apr 20 '22 at 00:47

0 Answers0