0

Goal: To create a Dynamic TabView that displays Todo Items filtered by their Category on each TabView. What is the best way to go about this in my current code? I don't want to modify the FetchRequest.

This is the TabView, I want to show the List of my Todos filtered on each Tab.

                TabBarView(currentTab: $currentTab, tabBarOptions: categoryTitles)
                
                TabView(selection: $currentTab) {
                    Text("List of todo items filtered by the Category Title").tag(0)
                    Text("tab2").tag(1)
                }
                .tabViewStyle(.page(indexDisplayMode: .never))

This is what my Todo Items currently look like unfiltered

                List {                   
                    ForEach(sections) { section in
                        Section(header: Text(section.id.capitalized)) {
                            ForEach(section) { todo in
                                TodoRowView(todo: todo)
                                    .frame(maxWidth: .infinity)
                                    .listRowSeparator(.hidden)
                            }
                            .onDelete { indexSet in
                                deleteTodo(section: Array(section), offsets: indexSet)
                            }
                        }
                    }
                    
                }
struct TabBarView: View {
    
    @Binding var currentTab : Int
    
    var tabBarOptions: [String] = ["Test1", "Test2", "Test3"]
    
    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 20 ) {
                ForEach(Array(zip(self.tabBarOptions.indices, self.tabBarOptions)),
                        id: \.0,
                        content: {
                    index, name in
                    TabBarItem(tabBarItemName: name, tab: index, currentTab: self.$currentTab)
                })
                
            }
        }
    }
}
@FetchRequest(entity: Category.entity(), sortDescriptors: []) var categories: FetchedResults<Category>

enter image description here

Justin Comstock
  • 175
  • 1
  • 13
  • Change your `body` to `TabView>ForEach>List>ForEach` vs `List>ForEach>Section>ForEach` – lorem ipsum Dec 24 '21 at 20:19
  • That gets the current list to show, but I still need the List on every tab, each tab has the same list but it should be filtered by the category. – Justin Comstock Dec 24 '21 at 20:47
  • This needs a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). You need to have some way to correlate the selected tab to the categories, and I don't see that in the code you have posted. It would also help to screenshot your Core Data `Category` entity. – Yrb Dec 24 '21 at 22:14
  • If you replace the current List with a TabView you will get a tab for every section. Just change the two words – lorem ipsum Dec 25 '21 at 12:22

0 Answers0