来自JSON文件的SwiftUI搜索列表

问题描述 投票:0回答:1

目前,我的SearchView在数组names上使用了一个简单的过滤器,我需要它才能使其遍历我的JSON文件。我有一个具有以下结构的JSON文件:

struct UcmData: Codable, Identifiable {
    let id: Int
    let building: [Building]
}

// MARK: - Building
struct Building: Codable, Identifiable {
    let id: Int
    let title, subtitle, info, image: String
    let floor: [Floor]
}

// MARK: - Floor
struct Floor: Codable, Identifiable {
    let id, number: Int
    let title, subtitle, image: String
    let cabinet: [Cabinet]?
}

// MARK: - Cabinet
struct Cabinet: Codable, Identifiable {
    let id: Int
    let number: String
    let person: [Person]
}

// MARK: - Person
struct Person: Codable, Identifiable {
    let id: Int
    let name: String
}

SearchView:

struct SearchView: View {

    let names = ["306 B", "doc. Ing. Michal Čerňanský, PhD."]
    let ucmData = Bundle.main.decode(UcmData.self, from: "ucm_data.json")
    @State private var searchText = ""
    @State private var showCancelButton: Bool = false


    var body: some View {
        VStack {
            HStack {
                HStack {
                    Image(systemName: "magnifyingglass")
                    TextField("Zadajte text pre vyhľadávanie", text: $searchText, onEditingChanged: { isEditing in
                        self.showCancelButton = true
                    }, onCommit: {
                        print("onCommit")
                    }).foregroundColor(.primary)
                    Button(action: {
                        self.searchText = ""
                    }) {
                        Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
                    }
                }
                .padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6))
                .foregroundColor(.secondary)
                .background(Color(.secondarySystemBackground))
                .cornerRadius(10.0)
                if showCancelButton  {
                    Button("Cancel") {
                            UIApplication.shared.endEditing(true)
                            self.searchText = ""
                            self.showCancelButton = false
                    }
                    .foregroundColor(Color(.systemBlue))
                }
            }
            .padding(.horizontal)
            .navigationBarHidden(showCancelButton)
            List {
                ForEach(self.names.filter{
                    self.searchText.isEmpty ? $0.localizedStandardContains("") :
                        $0.localizedStandardContains(self.searchText)
                }, id: \.self) { name in
                    Text(name)
                }
            }
            .navigationBarTitle(Text("Vyhľadávanie"))
            .resignKeyboardOnDragGesture()
        }
    }
}

extension UIApplication {
    func endEditing(_ force: Bool) {
        self.windows
            .filter{$0.isKeyWindow}
            .first?
            .endEditing(force)
    }
}

struct ResignKeyboardOnDragGesture: ViewModifier {
    var gesture = DragGesture().onChanged{_ in
        UIApplication.shared.endEditing(true)
    }
    func body(content: Content) -> some View {
        content.gesture(gesture)
    }
}

extension View {
    func resignKeyboardOnDragGesture() -> some View {
        return modifier(ResignKeyboardOnDragGesture())
    }
}

struct SearchView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
           SearchView()
              .environment(\.colorScheme, .light)

           SearchView()
              .environment(\.colorScheme, .dark)
        }
    }
}

如何将searchTextCabinet.numberPerson.name进行匹配,并在JSON文件中列出匹配项及其路径,例如是“ building.title> floor.title> cabinet.number”还是人“ building.title> floor.title> cabinet.number> person.name”?谢谢您的建议。

json swift search swiftui
1个回答
0
投票

我认为您必须为此编写一个搜索功能,并手动执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.