0

I've managed to build a user form to get inputs and display them in grids. Right now I have them displayed in a scroll view. I'm avoiding lists so as to get a card like appearance. However, I'm not able to get a delete feature to work as it is done with Lists. I'm new to swift and not sure how to implement delete feature in this grid layout. I would like for users to be able to delete entries. I'm using Core Data, Swift and SwiftUI. Thanks.

struct ContentView: View {
    // MARK: PROPERTIES
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @State private var showingAddChangeView: Bool = false
    @State private var dateAdded: Date = Date()
    @State private var showingActionSheet = false
    
   
    
    @FetchRequest(
        entity: Agent.entity(),
        sortDescriptors: [NSSortDescriptor(keyPath: \Agent.dateAdded, ascending: false)]) var cas: FetchedResults<Agent>
    
    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }()
    
    private var gridItemLayout = [GridItem(.flexible())]
    
    
    
    // MARK: BODY VIEW
    var body: some View {
        NavigationView {
            ScrollView {
                LazyVGrid(columns: gridItemLayout , alignment: .center, spacing: 30) {
                    ForEach(self.cas, id: \.self) { caRequest in
                        VStack(alignment: .leading) {
                            HStack {
                                Image(systemName: "calendar")
                                    
                                Text(caRequest.dateAdded ?? "Missing Dates")
                                    .font(.callout).fontWeight(.bold)
                                Spacer()
                                Button(action: {

                                }) {
                                    Image(systemName: "trash.fill")
                                        .foregroundColor(.black)
                                }
                                .padding(.trailing, 20)
                            }
                            //.padding()
                            .padding(.leading, 20)
                            .padding(.top, 25)
                            
                            Divider().frame(height: 1).background(Color.gray)
                                .padding()
                            
                            HStack{
                                Image(systemName: "paintbrush.fill")
                                    .foregroundColor(Color(.systemGreen))
                                    .padding(.leading, 20)
                                    .padding(.bottom, 5)
                                
                                Text(caRequest.intention ?? "No Intentions Recorded")
                                    .font(.callout).multilineTextAlignment(.leading)
                                    .padding(.leading, 5)
                                    .padding(.bottom, 5)
                                
                            }
                            
                            HStack{
                                Image(systemName: "checkmark.seal.fill")
                                    .foregroundColor(Color(.systemBlue))
                                    .padding(.leading, 20)
                                    .padding(.bottom, 5)
                                
                                Text(caRequest.affirmation ?? "No Affirmation Recorded")
                                    .font(.callout).multilineTextAlignment(.leading)
                                    .padding(.leading, 5)
                                    .padding(.bottom, 5)
                            }
                            HStack{
                                Image(systemName: "heart.fill")
                                    .foregroundColor(Color(.systemPink))
                                    .padding(.leading, 20)
                                    .padding(.bottom, 5)
                                
                                Text(caRequest.gratitudeOne ?? "Missing Gratitude")
                                    .font(.callout).multilineTextAlignment(.leading)
                                    .padding(.leading, 5)
                                    .padding(.bottom, 5)
                            }
                            HStack{
                                Image(systemName: "heart.fill")
                                    .foregroundColor(Color(.systemPink))
                                    .padding(.leading, 20)
                                    .padding(.bottom, 5)
                                
                                
                                Text(caRequest.gratitudeTwo ?? "Missing Gratitude")
                                    .font(.callout).multilineTextAlignment(.leading)
                                    .padding(.leading, 5)
                                    .padding(.bottom, 5)
                                
                            }
                            HStack{
                                Image(systemName: "heart.fill")
                                    .foregroundColor(Color(.systemPink))
                                    .padding(.leading, 20)
                                    .padding(.bottom, 20)
                                
                                
                                Text(caRequest.gratitudeThree ?? "Missing Gratitude")
                                    .font(.callout).multilineTextAlignment(.leading)
                                    .padding(.leading, 5)
                                    .padding(.bottom, 20)
                                
                            }
                        }
                        //  .frame(width: 350, height: 300)
                        .frame(minWidth: 0, maxWidth: 350, minHeight: 0, maxHeight: .infinity)
                        
                   
                       
                    } // END: FOR EACH
                } // END: LAZYVGRID
                
            } // END: SCROLLVIEW
            .padding(.top, 100)
           
            
            
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    HStack {
                        Image(systemName: "person.crop.square.fill")
                            .foregroundColor(.white)
                            .font(.title)
                        Text("Change Agent")
                            .foregroundColor(.white)
                            .font(.title2)
                            .fontWeight(.bold)
                            .shadow(radius: 20)
                    }
                }
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarItems(
                trailing:
                    Button(action: {
                        self.showingAddChangeView.toggle()
                    }) {
                        Text("Add")
                        Image(systemName: "plus")
                            .imageScale(.medium)
                        
                    } // END: ADD BUTTON
                    .foregroundColor(.white)
                    .sheet(isPresented: $showingAddChangeView) {
                        AddChangeView()
                        
                    }
                    .navigationBarItems(trailing: EditButton())
                
                        ) // END: NAVIGATION BAR ITEMS
            
            
        } // END: NAVIGATIONVIEW
        
    } // END: BODY VIEW

} // END: STRUCT
SaaSy Monster
  • 552
  • 2
  • 16
  • Grids do not have such as List built-in delete feature, you have to implement custom, like in https://stackoverflow.com/a/60893462/12299030, or in https://stackoverflow.com/a/63833510/12299030. – Asperi Feb 24 '21 at 14:03
  • Thanks @Asperi it looks like your solution on these threads as well. Can you tell me how that factors into my code piece above. As in the data in my app is dynamically created, stored in core data and then loaded on another view from the Form. Your code example is referencing a specific array. I tried implementing https://stackoverflow.com/questions/63824163/how-to-create-a-custom-delete-button-without-using-the-slide-to-delete-that-come/63833510#63833510 earlier today but could not get it to work. – SaaSy Monster Feb 24 '21 at 14:07

0 Answers0