在swiftui中放置搜索栏

在swiftui中放置搜索栏,swiftui,Swiftui,我正在尝试在我的第一个应用程序中设置搜索栏。我已经搜索过这个主题,我找到的所有内容都有一个数组作为常量,但我使用的是json import SwiftUI let menu = Bundle.main.decode([MenuSection].self, from: "menu.json") struct ContentView: View { @State var buscar: String = "" var body: some View { Naviga

我正在尝试在我的第一个应用程序中设置搜索栏。我已经搜索过这个主题,我找到的所有内容都有一个数组作为常量,但我使用的是json

import SwiftUI
let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")

struct ContentView: View {
    @State var buscar: String = ""
    var body: some View {
        NavigationView {
            VStack {

                List  {
                    ForEach(menu) { section in
                        Section(header: Busqueda(text: self.$buscar)) {
                            ForEach(section.items) {item in
                                detalleLinea(item: item)
                            }
                        }
                    }
                }.navigationBarTitle("Menu de opciones")
                    .listStyle(GroupedListStyle())
            }
        }
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
我已经尝试过不同的方法,但都没有效果<代码>部分(标题:Busqueda(文本:self.$buscar))有搜索栏,工作正常-我可以在我的应用程序中看到它,但我想在
ForEach
内部应用过滤器

ForEach(section.items) {item in
    detalleLinea(item: item)
}

它解决了谢谢你的帮助LuLuGaGa和所有人

struct ContentView: View {
    @State private var buscar: String = ""
    var body: some View {
        NavigationView {
            VStack {
                Busqueda(text: $buscar)
                List(menu.filter {
                    buscar.isEmpty ?
                        true :
                        "\($0)".localizedCaseInsensitiveContains(buscar)
                }, id: \.self) { item in
                    //                    ForEach(menu) {item1 in
                    detalleLinea(item: item)
                    //                    }
                }
            }.navigationBarTitle("Menu de opciones")
            //                 .listStyle(GroupedListStyle())
        }
    }
}

它解决了谢谢你的帮助LuLuGaGa和所有人

struct ContentView: View {
    @State private var buscar: String = ""
    var body: some View {
        NavigationView {
            VStack {
                Busqueda(text: $buscar)
                List(menu.filter {
                    buscar.isEmpty ?
                        true :
                        "\($0)".localizedCaseInsensitiveContains(buscar)
                }, id: \.self) { item in
                    //                    ForEach(menu) {item1 in
                    detalleLinea(item: item)
                    //                    }
                }
            }.navigationBarTitle("Menu de opciones")
            //                 .listStyle(GroupedListStyle())
        }
    }
}

有什么问题?请不要发布你的代码截图,请在你的问题中添加代码。你的问题太广泛了。很难理解您想要实现什么(您想把搜索栏放在哪里?)以及什么阻碍了您这么做(它是否没有显示?您不知道在哪里查找文档?是否存在编译器错误?)。您的代码不包含任何试图插入条形图的行。请说得更准确些。在这一行中,我有搜索栏,并在我想要的地方找到了答案。这应该会有所帮助:问题已经解决了。。像这样,谢谢大家的帮助有什么问题吗?请不要发布你的代码截图,请在你的问题中添加代码。你的问题太广泛了。很难理解您想要实现什么(您想把搜索栏放在哪里?)以及什么阻碍了您这么做(它是否没有显示?您不知道在哪里查找文档?是否存在编译器错误?)。您的代码不包含任何试图插入条形图的行。请说得更准确些。在这一行中,我有搜索栏,并在我想要的地方找到了答案。这应该会有所帮助:问题已经解决了。。像这样,谢谢大家的帮助