2

我正在构建一个旅行计划应用程序,允许用户使用 Picker 从本地 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"
  }]
4

1 回答 1

2

你真的需要选择器吗?如果没有,你可以试试这个例子。让我们知道 UI 是否仍然存在问题。

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
}
于 2021-05-18T06:31:59.140 回答