我是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]

推荐答案

您应该使用@State来跟踪当前列表显示的项目:

@State private var favorites: [FavoriteModel] = myFavorites

如果myFavorites不是全局常量,而是self中的其他常量,则可以在onAppear中指定它.

在你的ForEachE中使用这@State:

ForEach(favorites) { favorite in
    // ...
}

searchText发生变化时,只需根据搜索文本更新状态.

.onChange(of: searchText) { search in
    updateFilteredFavorites(search)
}

您可以在updateFilteredFavorites中进行任何类型的过滤.例如:

func updateFilteredFavorites(_ searchText: String) {
    if searchText == "" {
        favorites = myFavorites
        return
    }
    
    var temp = myFavorites
    for i in temp.indices {
        temp[i].elements = temp[i].elements.filter { $0.name.localizedStandardContains(searchText) }
    }
    // Remove the empty sections
    favorites = temp.filter { !$0.elements.isEmpty }
}

Swift相关问答推荐

对多个项目的数组进行排序

map 视图上的SwiftUI渐变折线

SwiftUI轨迹绘制怪异

阻塞(受CPU限制的)任务的异步功能?

如何使用AsyncTimerSequence获取初始时钟,然后在指定的时间间隔内开始迭代?

如何在SwiftUI中做出适当的曲线?

';NSInternal不一致异常';,原因:';可见导航栏Xcode 15.0 Crash请求布局

使用`JSONSerialiser`时,省略通用可选项

暂停我的 AR 会话并清除变量和锚点的功能

如何在 Swift 中做类型安全的索引?

SwiftUI 无法在实时活动中显示 SpriteView

如何更新 UserDefault 中的特定值

类型 '()' 不能符合 View (除非它肯定是 View,这次没有恶作剧)

将数据附加到 Firebase 实时数据库后,NavigationLink 将我重定向到上一个视图

macOS 守护进程应该由Command Line ToolXcode 模板制作吗?

FCM 后台通知在 iOS 中不起作用

如何发送静默推送通知有效负载

使用 Swift 以编程方式自定义 UITableViewCell

使用相机进行人脸检测

将 UIImage 剪成圆形