swift - 通过大数组 Swift 进行过滤

标签 swift swiftui swiftui-list swiftui-form

我正在构建一个旅行计划应用程序,让用户使用选择器从本地 json 文件中选择城市。

json 文件有 115K 个项目,如果我尝试过滤这么多项目,UI 就会卡住。有关如何提高效率的任何建议。

任何帮助将不胜感激。

import SwiftUI

struct AddTripView: View {

    let cities = Bundle.main.decode([Destination].self, from: "cities.json")
    @State private var selectedCity = 0
    @State var searchText = ""

       var body: some View {
        
            Form {
                Section {
                    Picker(selection: $selectedCity, label: Text("Select city")) {
                        
                        SearchBar(text: $searchText)
 
                        ForEach(cities.filter {searchText.isEmpty ? true : $0.city.lowercased().hasPrefix(searchText.lowercased())}, id: \.self) {
                            
                            Text($0.city)
                        }
                    }
                }
            }.navigationBarTitle("Create trip")
            
        }
}

struct AddTripView_Previews: PreviewProvider {
    static var previews: some View {
        AddTripView()
    }
}

JSON 示例

[{
    "id": 1,
    "city": "Zaranj",
    "country": "Afghanistan"
  },
  {
    "id": 2,
    "city": "Taloqan",
    "country": "Afghanistan"
  },
  {
    "id": 3,
    "city": "Shīnḏanḏ",
    "country": "Afghanistan"
  },
  {
    "id": 4,
    "city": "Shibirghān",
    "country": "Afghanistan"
  },
  {
    "id": 5,
    "city": "Shahrak",
    "country": "Afghanistan"
  }]

最佳答案

你真的需要一个选择器吗?如果没有,你可以尝试这个例子。 如果用户界面仍然存在问题,请告诉我们。

struct AddTripView: View {
    @State private var cities = [Destination]()
    @State private var selectedCity: Destination?
    @State private var searchQuery: String = ""
    
    var body: some View {
        VStack {
            Text("\(cities.count)")
            HStack {
                TextField("city search", text: $searchQuery).padding(5)
                    .overlay(RoundedRectangle(cornerRadius: 15).stroke(Color.blue, lineWidth: 1))
                    .foregroundColor(.blue)
                    .frame(width: 160)
                Button(action: {searchQuery = ""}) {
                    Image(systemName: "xmark.circle").font(.title)
                }
            }.padding(20)
            Divider()
            Text("Select city")

        ScrollView {
            LazyVStack (alignment: .leading) {
                ForEach(cities.filter{self.searchFor($0.city)}.sorted(by: { $0.city < $1.city })) { city in
                    Text("\(city.city)  \(city.country)")
                        .onTapGesture {
                            selectedCity = city
                            print("---> selectedCity: \(selectedCity?.city)")
                        }
                }
            }
        }.onAppear() {
                 cities = Bundle.main.decode([Destination].self, from: "cities.json")
            }
        }
    }
    
    private func searchFor(_ txt: String) -> Bool {
        return (txt.lowercased(with: .current).hasPrefix(searchQuery.lowercased(with: .current)) || searchQuery.isEmpty)
    }
}

struct Destination: Codable, Identifiable, Equatable {
    var id: Int
    var city: String
    var country: String
}

关于swift - 通过大数组 Swift 进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67579561/

相关文章:

swiftui - 如何使一个 View 中的两个列表在 SwiftUI 中作为一页滚动?

ios - Google map 在 iOS 开发中支持 Apple Watch 吗?

ios - 如何使英雄 Sprite 的边框适合

swift - 如何在 WidgetKit 预览 SwiftUI iOS 14 中加载 WKWebView

ios - SwiftUI 中的 Gridview

ios - 在 SwiftUI 中为圆角矩形创建均匀分布的虚线轮廓

ios - SwiftUI 应用程序可以在预览版中运行,但不能在模拟器中运行

ios - 使用 cocoapod 的 swift 应用程序在 20 点后无法启动

ios - 将字典从 Watch 发送到 App - WatchConnectivity

swiftui - 如何使 SceneView 的背景透明?