如何仅显示列表 Swiftui 的可搜索结果

How to show only the result of a searchable of a List Swiftui

我正在输入列表编号,但我只想显示我的可搜索结果而不是整个列表,我该如何隐藏列表?

我试图在搜索文本为空时更改布尔值的状态,但 xcode 发送了一些类似 “在视图更新期间修改状态,这将导致未定义的行为”

谢谢!

import SwiftUI
import UIKit

struct NumberList: View {
    
    @EnvironmentObject var network: Network
    @State private var searchText = ""

    
    var body: some View {
        NavigationView {
            List(searchResults, id: \.id) { phones in
                NavigationLink {
                    NumberDetail(phones: phones)
                } label: {
                    VStack(alignment: .leading) {
                        Text(phones.Number)
                            .bold()
                    }
                }
            }
            .task{
                network.getnumbers()
            }
            .searchable(text: $searchText)
            .navigationTitle("Numbers")
            .keyboardType(UIKeyboardType.numberPad)
        }

        .background(
             Image("background")
                 .resizable()
                 .scaledToFill()
        )
        .opacity(0.75)
    }

    var searchResults: [phonesExt] {
        if searchText.isEmpty {
            return network.phones
        } else {
            return network.phones.filter { [=10=].number.contains(searchText) } 
        }
    }

}

struct DelegateApp: App {
    var body: some Scene { WindowGroup{ NumberList()}}
}
 
 struct NumberList_Previews: PreviewProvider {
     static var previews: some View {
         NumberList().environmentObject(Network())
     }
 }```

Craft search experiences in SwiftUI that explains the isSearching 环境变量中有一些提示。虽然显示的代码非常模糊。

NavigationView {
    WeatherList(text: $text) {
        ForEach(data) { item in
            WeatherCell(item)
        }
    }
}
.searchable(text: $text)

注意:我非常怀疑 ForEach 是否显示在其中,它更有可能在 WeatherList 或 sub-views.

之一内
struct WeatherList: View {
    @Binding var text: String
    
    @Environment(\.isSearching)
    private var isSearching: Bool
    
    var body: some View {
        WeatherCitiesList()
            .overlay {
                if isSearching && !text.isEmpty {
                    WeatherSearchResults()
                }
            }
    }
}

注意:他提供绑定而不需要写入权限这一事实让我担心这位演示者不知道 SwiftUI。

经过大量搜索,我发现了很多东西,这对我有用

我使用修饰符 onChange 来查看我的 texfield 的状态

 var body: some View {
    NavigationView {
        List(searchResults, id: \.id) { phones in
            NavigationLink {
                NumberDetail(phones: phones)
            } label: {
                VStack(alignment: .leading) {
                    Text(phones.Number)
                        .bold()
                }
            }
        }
        .listStyle(.plain)
        .searchable(text: $searchText)
        .onChange(of: searchText) { value in
            if !value.isEmpty && value.count < 9 {
          //value belongs to searchtext so if the searchtext is empty value too
                network.getNumbers()
            }else {
                network.phones.removeAll()
                print("Clean the list")
               }
        }
        .navigationTitle("Numbers")
        .keyboardType(UIKeyboardType.numberPad)
    }

    .background(
         Image("background")
             .resizable()
             .scaledToFill()
    )
    .opacity(0.75)
}