Search code examples
listswiftuifilteringsearchbar

How to filter only after clicking the "Search" button on the keyboard


I have a long list to filter. Now the search is executed after every character entered in the searcher. This takes some seconds for every character entered. To fix this problem I would like to execute the search only after the "search" Button is clicked on the keyboard.

This is the ContentView with searchbar:

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)
                    }
                }

This is the searcher structure:

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
    }
}

Solution

  • You can simply change the @Binding text when that button is clicked

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