【问题标题】:How to filter only after clicking the "Search" button on the keyboard如何仅在单击键盘上的“搜索”按钮后进行过滤
【发布时间】:2021-01-24 10:30:02
【问题描述】:

我有一个很长的列表要过滤。现在在搜索器中输入每个字符后执行搜索。每个输入的字符都需要几秒钟。为了解决这个问题,我想只在点击键盘上的“搜索”按钮后执行搜索。

这是带有搜索栏的 ContentView:

VStack {
                SearchBar(text: $searchTerm, placeholder: "Suche")
            }
            
         
                List {
                    
                    ForEach(gesetzestextTEMPO.filter {
                        self.searchTerm.isEmpty ? true : $0.titel1.localizedCaseInsensitiveContains(searchTerm)
                            || $0.titel1.localizedCaseInsensitiveContains(searchTerm)
                            || $0.artikel.localizedCaseInsensitiveContains(searchTerm)
                            || $0.marginale.localizedCaseInsensitiveContains(searchTerm)
                            || $0.absatz0.localizedCaseInsensitiveContains(searchTerm)
                            || $0.absatz0litaz.localizedCaseInsensitiveContains(searchTerm)
                          
                        
                        })
                    { item in
                        Part13(gesetzestextTEMPO: item, searchTerm: self.$searchTerm)
                    }
                }

这是搜索器结构:

import Foundation
import SwiftUI

struct SearchBar: UIViewRepresentable {
    
    @Binding var text: String
    var placeholder: String
    
    
    
    class Coordinator: NSObject, UISearchBarDelegate {
        
        @Binding var text: String
        @Published var searchTerm: String = ""
        
        
        init(text: Binding<String>) {
            _text = text
            
        }
        
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
            
        }
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            searchBar.resignFirstResponder()
        }
    }
    
    func makeCoordinator() -> SearchBar.Coordinator {
        return Coordinator(text: $text)
    }
    
    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        searchBar.placeholder = placeholder
        searchBar.searchBarStyle = .minimal
        searchBar.autocapitalizationType = .none
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.text = text
    }
}

【问题讨论】:

    标签: list swiftui filtering searchbar


    【解决方案1】:

    您可以在单击该按钮时简单地更改@Binding 文本

    //        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    //            text = searchText
    //        }
            
            func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
                if let textToSearch = searchBar.text {
                    text = textToSearch
                }
                
                searchBar.resignFirstResponder()
            }
    

    【讨论】:

    • 嗨@iSoldier,如果这个或任何答案已经解决了您的问题,请考虑通过单击复选标记接受它。
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2020-05-04
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多