我需要通過從 SearchBar 搜索來過濾 ForEach 中的系結,但它給了我一個錯誤。我不確定這在 SwiftUI 中是否可行,但如果您知道我可以過濾的方式,請告訴我。我真的很感激任何幫助。
這是我在 ForEach 中放入的結構體的樣子:
struct Test: Identifiable {
let id = UUID()
var number: Int
var text: String
}
這是使用 ForEach 的視圖:
struct ContentView: View {
@State var test = [
Test(number: 1, text: "1"),
Test(number: 2, text: "2"),
Test(number: 3, text: "3"),
Test(number: 4, text: "4"),
Test(number: 5, text: "5"),
Test(number: 6, text: "6")
]
@State private var searchText = ""
@State private var placeholder = "Search..."
var body: some View {
NavigationView{
VStack{
SearchBar(text: $searchText, placeholder: $placeholder)
List {
ForEach($test.filter {
($0.text.lowercased().contains(searchText.lowercased()) || searchText == ""}) { $data in
TestView(test: $data)
}
}
}
}
}
}
這是我的搜索欄:
struct SearchBar: UIViewRepresentable {
@Binding var text: String
@Binding var placeholder: String
class Coordinator: NSObject, UISearchBarDelegate {
@Binding var text: String
@Binding var placeholder: String
init(text: Binding<String>, placeholder: Binding<String>) {
_text = text
_placeholder = placeholder
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
text = searchText
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(false, animated: true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
}
func makeCoordinator() -> SearchBar.Coordinator {
return Coordinator(text: $text, placeholder: $placeholder)
}
func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
let sb = UISearchBar(frame: .zero)
sb.placeholder = context.coordinator.placeholder
sb.delegate = context.coordinator
return sb
}
func updateUIView(_ uiView: UISearchBar, context: Context) {
uiView.text = text
}
}
uj5u.com熱心網友回復:
我認為這個問題是不是過濾本身,但.lowercased()將ForEach被拒絕。有一個更簡單的方法,而不是試圖強迫它。使用執行過濾的計算變數,然后滾動您自己的Binding以發送到視圖,如下所示:
struct ContentView: View {
@State var testArray = [
Test(number: 1, text: "1"),
Test(number: 2, text: "2"),
Test(number: 3, text: "3"),
Test(number: 4, text: "4"),
Test(number: 5, text: "5"),
Test(number: 6, text: "6")
]
@State private var searchText = ""
@State private var placeholder = "Search..."
var filteredTest: [Test] {
// If you want to return no items when there is no matching filter use this:
testArray.filter { ($0.text.lowercased().contains(searchText.lowercased()))}
// If you want the whole array unless the filter matches use this:
let returnTestArray = testArray.filter { ($0.text.lowercased().contains(searchText.lowercased()))}
guard !returnTestArray.isEmpty else { return testArray }
return returnTestArray
}
var body: some View {
NavigationView{
VStack{
SearchBar(text: $searchText, placeholder: $placeholder)
List {
ForEach(filteredTest) { test in
TestView10(test: Binding<Test> (
get: { test },
set: { newValue in
if let index = testArray.firstIndex(where: { $0.id == newValue.id } ) {
testArray[index] = newValue
}
}
))
}
}
}
}
}
}
我也改名為您的資料陣列testArray,以更好地反映這是什么,以及在ForEach,data現在變成test。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/381574.html
上一篇:使用表單SwiftUI的導航鏈接
