對 Xcode 進行一些更改,當它不是基本串列時,我對如何制作可搜索的搜索欄感到有些困惑。我有一個帶有識別符號串列的頁面和另一個帶有嵌入串列代碼的頁面。
希望有人在那里比我有更多的 nolage。
第 1 頁
import SwiftUI
struct Country: Identifiable {
let id = UUID()
let imageName: String
let title: String
let description: String
let viewCount: Int
let uploadDate: String
let url: URL
}
struct CountryList {
static let AllCountries = [
Country(imageName: "flag-of-Sweden",
title: "Sweden",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 370222,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Poland",
title: "Poland",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 239150,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Russia",
title: "Russia",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 162897,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Spain",
title: "Spain",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 119115,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Libya",
title: "Libya",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 112213,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
第 2頁 這是第 2 頁,我將串列實施到應用程式
import SwiftUI
struct CountryListView: View {
var country: [Country] = CountryList.AllCountries
@State private var searchText = ""
var body: some View {
NavigationView {
List(country, id: \.id) { country in
NavigationLink(destination: CountryDetailView(Country: country), label: {
CountryCell(Country: country)
})
}
.navigationTitle("Find your country")
.searchable(text: $searchText)
}
}
struct CountryCell: View {
var Country: Country
var body: some View {
HStack {
Image(Country.imageName)
.resizable()
.scaledToFit()
.frame(height: 70)
.cornerRadius(16)
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 5) {
Text(Country.title)
.fontWeight(.semibold)
.lineLimit(2)
.minimumScaleFactor(0.5)
Text(Country.uploadDate)
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
var searchResults: [String] {
if searchText.isEmpty {
return Country
} else {
return Country.filter({ $0.contains(searchText)})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
CountryListView()
}
}
uj5u.com熱心網友回復:
您的代碼中有許多不正確的地方。嘗試/研究此代碼以實作您想要的。
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
CountryListView()
}
}
struct Country: Identifiable {
let id = UUID()
let imageName: String
let title: String
let description: String
let viewCount: Int
let uploadDate: String
let url: URL
}
struct CountryList {
static let AllCountries = [
Country(imageName: "flag-of-Sweden",
title: "Sweden",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 370222,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Poland",
title: "Poland",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 239150,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Russia",
title: "Russia",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 162897,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Spain",
title: "Spain",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 119115,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
Country(imageName: "flag-of-Libya",
title: "Libya",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 112213,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
]
}
struct CountryListView: View {
@State var countries: [Country] = CountryList.AllCountries
@State private var searchText = ""
var body: some View {
NavigationView {
List(searchResults) { country in
NavigationLink(destination: CountryDetailView(Country: country), label: {
CountryCell(country: country)
})
}
.navigationTitle("Find your country")
.searchable(text: $searchText)
}.navigationViewStyle(.stack)
}
// adjust according to your needs
var searchResults: [Country] {
let textSearch = searchText.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
if searchText.isEmpty {
return countries
} else {
return countries.filter{ $0.title.trimmingCharacters(in: .whitespacesAndNewlines).lowercased().starts(with: textSearch) }
}
}
}
struct CountryCell: View {
var country: Country
var body: some View {
HStack {
Image(country.imageName)
.resizable()
.scaledToFit()
.frame(height: 70)
.cornerRadius(16)
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 5) {
Text(country.title)
.fontWeight(.semibold)
.lineLimit(2)
.minimumScaleFactor(0.5)
Text(country.uploadDate)
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
uj5u.com熱心網友回復:
歡迎使用堆疊溢位!請參觀并查看:我如何提出一個好問題?以及如何創建最小的、可重現的示例。
每當您使用串列時,搜索欄及其模式都不會改變。您感到困惑的地方是不明白您回傳的是資料型別的陣列,而不僅僅是字串。[String]通常用作示例,因為它們很簡單,但混淆來自于您的搜索文本也是String. 真的,您正在回傳一個[Type]whereType是您的資料型別。在這種情況下,這是一個Country. 因此你的代碼變成這樣:
struct CountryListView: View {
// You should just declare this as @State var, NEVER just var in a struct.
// If you are not planning to mutate this list, declare it with let
// Also, you reused "Country" which caused the compiler to be confused.
// Types should begin with a capital letter and be singular
// Names of singular instances should be lowercased.
// Names of arrays of a type should be lowercased and plural
// You will see I changed the instance you called Country to countries.
@State private var countries = CountryList.allCountries
@State private var searchText = ""
var body: some View {
NavigationView {
// The list uses searchResults which is an [Country]
// Since Country is Identifiable, you do not need the id:\.id in the list.
List(searchResults) { country in
// You didn't provide CountryDetail, so I used CountryCell for both
NavigationLink(destination: CountryCell(country: country), label: {
CountryCell(country: country)
})
}
.navigationTitle("Find your country")
.searchable(text: $searchText)
}
}
var searchResults: [Country] {
if searchText.isEmpty {
return countries
} else {
return countries.filter({ $0.title.lowercased().contains(searchText.lowercased())})
}
}
struct CountryCell: View {
let country: Country
var body: some View {
HStack {
// Since we don't have the flag assets, I just used an SF Symbol
// Please do this sort of substitution before you post.
Image(systemName: country.imageName)
.resizable()
.scaledToFit()
.frame(height: 70)
.cornerRadius(16)
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 5) {
Text(country.title)
.fontWeight(.semibold)
.lineLimit(2)
.minimumScaleFactor(0.5)
Text(country.uploadDate)
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
}
struct CountryList {
static let allCountries = [
// Substitued SF Font for imageName
Country(imageName: "flag",
title: "Sweden",
description: "lorumibsum lorum ibsum sim sum sam",
viewCount: 370222,
uploadDate: "date of post",
url: URL(string: "https://test.com")!),
...]
}
一些額外的想法:
下次發布代碼之前,請在另一個專案中將其簡化為盡可能小,但仍會顯示您的問題。這段代碼中缺少很多東西,還有很多不相關的附加行。
此外,請注意他評論中概述的命名約定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388212.html
