Sunday, May 12, 2024
HomeiOS Developmentios - Looping sections with grouped values from mannequin

ios – Looping sections with grouped values from mannequin


I’m making an attempt to position medication beneath sections correlating to their class however im getting a number of sections for every drug I add of the identical class. Im unsure how a lot this course of differs when utilizing mannequin macros.

Present Consequence

   Listing {
    ForEach(activeStash) { drug in
        Part(header: Textual content(drug.substanceClass)) {
            NavigationLink(vacation spot: StashLinkView(stashed: drug)) {
                Textual content(drug.substance)
            }
        }
    }
}

Grouping however with duped sections Consequence:

Listing {
    ForEach(activeStash) { drug in
        Part(header: Textual content(drug.substanceClass)) {
            ForEach(activeStash) { drug in
                NavigationLink(vacation spot: StashLinkView(stashed: drug)) {
                    Textual content(drug.substance)
                }
            }
        }
    }
}

Resolution:

Listing {
ForEach(Array(Set(activeStash.map { $0.substanceClass })), id: .self) { substanceClass in
    Part(header: Textual content(substanceClass)) {
        ForEach(activeStash.filter { $0.substanceClass == substanceClass }) { drug in
            NavigationLink(vacation spot: StashLinkView(stashed: drug)) {
                Textual content(drug.substance)
            }
        }
    }
}

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments