Remove list headers in .searchable

Hi there,

I am still quite new to SwiftUI and made a .searchable list as you can see in my code below. When I search, all list headers are displayed, even if there aren't any results in that list. How can I fix that? Thank you for your help!

Laurin

    
    //Herangehensweise
                      "basismaßnahmen",
                      "cabcde herangehensweise",
                      "cabcde instabilitäten",
                      "beurteilung der bewusstseinslage",
                      "sampler",
                      "opqrst",
                      "atemwegsmanagement",
                      "patientenanmeldung",
                      
    //Kreislaufstillstand
                      "erwachsene bls reanimation",
                      "erwachsene als mit manueller defibrillation reanimation",
                      "erwachsene als mit aed reanimation",
                      "kinder pls reanimation",
                      "neugeborene pls baby reanimation",
                      "postreanimationstherapie rosc",
                      
    //Leitsymptome
                      "dyspnoe atemnot luft",
                      "kritische blutung tourniquet",
                      "kurzzeitige bewusstlosigkeit synkope tloc",
                      "nichttraumatischer brustschmerz thorax acs akutes aortensyndrom lungenembolie lae spontanpneumothorax spannungspneumothorax",
                      "schock blut volumenmangel blutdruck hypoton blutverlust",
                      "starke schmerzen nrs analgesie analgosedierung midazolam dormicum esketamin ketanest",
                      "zentrales neurologisches defizit bewusstlosigkeit",
                      
    //Krankheitsbilder
                      //More entries...
                      
                      
]

struct BPR: View {
    
    var bprs: [String] {
        
        let lcBprs = listOfBPRs.map { $0.lowercased() }
        
        return searchText == "" ? lcBprs : lcBprs.filter {
            $0.contains(searchText.lowercased())
        }
        
    }
    
    private var listOfBPRs = bprlist
    @State var searchText = ""
    
    var body: some View {
        
        NavigationView {
            List {
                Group {
                    Section (header: Text("Herangehensweise")){
                        ForEach(bprs, id: \.self) { bpr in
                            if bpr.contains("basismaßnahmen") {
                                
                                NavigationLink(destination: Basismassnahmen()) {
                                    Label {
                                        Text("Basismaßnahmen")
                                    } icon: {
                                        Image(systemName: "list.bullet.clipboard")
                                    }
                                }
                            } else if bpr.contains("cabcde herangehensweise"){
                                NavigationLink(destination: cabcdeHerangehensweise()) {
                                    Label {
                                        Text("cABCDE-Herangehensweise")
                                    } icon: {
                                        Image(systemName: "abc")
                                    }
                                }
                            } else if bpr.contains("cabcde instabilitäten"){
                                NavigationLink(destination: cabcdeInstabilitaeten()) {
                                    Label {
                                        Text("cABCDE-Instabilitäten")
                                    } icon: {
                                        Image(systemName: "textformat.abc.dottedunderline")
                                    }
                                }
                            } //More else if statements and groups...
                            }
                        }
                        
                    }
                    
                }
                
            }
            
            .searchable(text: $searchText)
            .disableAutocorrection(true)
            .background(Color.clear)
            .listStyle(SidebarListStyle())
            .navigationTitle("Behandlungspfade")
            .navigationBarTitleDisplayMode(.automatic)
            .toolbar {
                NavigationLink(destination: About()){
                    Image(systemName: "info.circle")
                }
                NavigationLink(destination: Settings()){
                    Image(systemName: "gear")
                }
            }
            
            BackgroundMedikamente()
        }
        
    }
    
}

Hi @rinuality ,

A few things here:

  • first of all, please use NavigationStack instead of NavigationView (although if you're supporting iOS 15, keep NavigationView)
  • the reason your headers are still showing up is because they are not inside of your ForEach loop. If you move the Section inside the loop, they will not show up if there are no results
  • you're using a lot of if else instead of utilizing the programmatic nature of a ForEach loop. This line: ForEach(bprs, id: \.self) { bpr in gives you a variable called bpr that is the String value of each String in your array. You can use this instead of hardcoding the Strings into your NavigationLink. This becomes complicated though in what your destination should be for each value and how many values each section should be for. I'll cover this below

These are some top-level reasons why you're seeing these issues, but another thing I'd like to cover here is the way you're parsing your data. You're doing an awesome job so far, I know you just started learning, so I want to show you this tutorial about how to use models and structs to store your data instead of an enormous array that you have to use if else statements to parse. The tutorial starts with hardcoded data and teaches you how to build a model for navigation, so I think it will be really helpful.

Good luck!

Hi @sha921 ,

thank you again for your help. Unfortunately I have a few problems implementing your code: -> When I use NavigationStack instead of NavigationView, it looks like this:

The reason for this is, that I implemented a background for iPad use, where it should look like this:

How can I implement this with NavigationStack?

-> The other problem is, that it looks like this, when I move the Section inside the loop:

I moved the Section directly underneath the if statement, like this:

ForEach(bprs, id: \.self) { bpr in
Section (header: Text("Herangehensweise")){
if bpr.contains("basismaßnahmen") {

Is that wrong? Thank you for your help!

Laurin

Remove list headers in .searchable
 
 
Q