我是SwiftUI的新手,我正在努力学习.在我的测试项目中,我想在List
中进行搜索,但在我看到的教程中,没有任何关于在另一个文件中使用struct
模型的教程.我不知道该怎么处理.我试着写一些东西,但没有成功.你能帮我处理一下吗?首先要感谢你的帮助.我将我的代码放在下面以供判断:
ContentView.swift
import SwiftUI
struct ContentView: View {
@State private var searchText: String = ""
@State private var filteredText: [String] = []
var body: some View {
NavigationView {
VStack {
List {
ForEach(myFavorites) {favorite in
Section(header: Text(favorite.title)) {
ForEach(favorite.elements) { element in
NavigationLink(destination: DetailsView(chosenFavoriteElement: element)) {
Text(element.name)
}
}
}
}
}.navigationBarTitle("Favorite Book App").navigationBarTitleDisplayMode(.inline)
.searchable(text: $searchText).onChange(of: searchText) { search in
//I didn't wrote anyting down here
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FavoriteModel.swift
import Foundation
struct FavoriteModel : Identifiable {
var id = UUID()
var title : String
var elements : [FavoriteElements]
}
struct FavoriteElements : Identifiable {
var id = UUID()
var name : String
var imageName : String
var description : String
}
let macOS = FavoriteElements(name: "MacOS", imageName: "macosventura", description: "MacOS 13
Ventura")
let windows = FavoriteElements(name: "Windows", imageName: "windows11", description: "Windows 11")
let borusanMannesmann = FavoriteElements(name: "Borusan Mannesmann", imageName: "borusanlogo", description: "Borusan Mannesmann Boru A.Ş.")
let aselsan = FavoriteElements(name: "Aselsan", imageName: "aselsan", description: "Aselsan Askeri Sanayi")
let favoriteOperatingSystems = FavoriteModel(title: "Favorite Operating Systems", elements: [macOS, windows])
let favoriteCompanies = FavoriteModel(title: "Favorite Companies", elements: [borusanMannesmann, aselsan])
let myFavorites = [favoriteOperatingSystems, favoriteCompanies]