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
2
  • Grids do not have such as List built-in delete feature, you have to implement custom, like in stackoverflow.com/a/60893462/12299030, or in stackoverflow.com/a/63833510/12299030. Commented Feb 24, 2021 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 stackoverflow.com/questions/63824163/… earlier today but could not get it to work. Commented Feb 24, 2021 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.