How can I use Expand and Collapse List Rows with Core Data elements?

Viewed 196
struct AppointmentView: View
{
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(entity: Appoint.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Appoint.title , ascending: true)])
    var appointments_title: FetchedResults<Appoint>
    @State var showAddDate = false
    @State var sectionState: [Int: Bool] = [:]
    
    var body: some View
    {
        NavigationView
        {
            List
            {
                    ForEach(appointments_title)
                    {
                        order in
                        HStack
                        {
                            Text("\(order.title)").font(.headline)
                            Spacer()
                            Text("\(order.date, formatter: ContentView.self.taskDateFormat)").font(.headline)
                        }.contentShape(Rectangle())
                            .onTapGesture
                            {
                                self.sectionState[order] = !self.isExpanded(order)
                            }
                        if self.isExpanded(order)
                        {
                            Text("\(order.description)”)
                        }
                    }
             }
         
            .navigationBarTitle("My Appointment")
            .navigationBarItems(trailing: Button(action: {self.showAddDate = true}, label: {Image(systemName: "plus.circle").resizable().frame(width: 32, height: 32, alignment: .center)}))
            .sheet(isPresented: $showAddDate) {AddDate().environment(\.managedObjectContext, self.managedObjectContext)}
        }
    }
    func isExpanded(_ order:Int) -> Bool
    {
        sectionState[order] ?? false
    }
}

When I try to run this code, I will get this error message;
Cannot convert value of type 'FetchedResults < Appoint >' to expected
argument type 'Range < Int >'

Is there a way to convert 'FetchedResults < Appoint > ' into 'Range < Int >' ?

1 Answers

I figured out something and it works. Basically, I just add another Attribute of type Boolean and call it 'isExpanded' in my .xcdatamodeld file. Every time I add a new entry, I will set its boolean attribute to false.

struct AppointmentView: View
{
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(entity: Appoint.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Appoint.title , ascending: true)])
    var appointments_title: FetchedResults<Appoint>
    @State var showAddDate = false
    //@State var sectionState: [Int: Bool] = [:]
    
    var body: some View
    {
        NavigationView
        {
            List
            {
                    ForEach(appointments_title)
                    {
                        order in
                        HStack
                        {
                            Text("\(order.title)").font(.headline)
                            Spacer()
                            Text("\(order.date, formatter: ContentView.self.taskDateFormat)").font(.headline)
                        }.contentShape(Rectangle())
                            .onTapGesture
                            {
                                order.isExpanded.toggle()
                            }
                        if order.isExpanded
                            {
                                Text("\(order.description)”)
                            }
                            else
                            {
                                EmptyView()
                            }
                    }
             }
         
            .navigationBarTitle("My Appointment")
            .navigationBarItems(trailing: Button(action: {self.showAddDate = true}, label: {Image(systemName: "plus.circle").resizable().frame(width: 32, height: 32, alignment: .center)}))
            .sheet(isPresented: $showAddDate) {AddDate().environment(\.managedObjectContext, self.managedObjectContext)}
        }
    }
    /*func isExpanded(_ order:Int) -> Bool
    {
        sectionState[order] ?? false
    }*/
}
Related