ios – Dynamically Hiding Empty Sections of List in SwiftUI


I have a dynamic SwiftUI List that populates utilizing an enum for States. Then it cycles through my dictionary of data looking for a state match to display data. All of this works perfectly. What I am looking to do but struggling with is to hide those states that are then empty.

The section of code in question is:

List {
    ForEach(USStates.allCases) { states in
        Section {
            ForEach (bucketList) { item in
                if (item.state == states.abrev) {
                    NavigationLink(destination: ItemDetails(blItem: item.recordID)) {
                        Label(item.name, systemImage: item.monogram)
                                    .frame(height: 50)
                            .foregroundColor(item.completedate=="0000-00-00" ? .white : .red)
                    }
                }
            }
        } header: {
            Text(states.rawValue)
        }
    }
}

I have tried using Visibility but can’t quite get the results I’m looking for. When a state doesn’t have a match, then I want that entire section to be hidden.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img