Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search when search button clicked in keyboard?

I have a searchbar in my tableview header. For that i am using uisearchcontroler. But it update tablview data when i am texting in searchbar, i need to update tablview when search button in keyboard is clicked, because i get data for search in api and everytime when i am texting in searchbar it requesting and it takes long time. How i can resolve this?

var resultSearchController = UISearchController()
var indicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
var ref=UIRefreshControl()
override func viewDidLoad() {
    super.viewDidLoad()


    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    indicator.center = view.center
    view.addSubview(indicator)
    indicator.bringSubviewToFront(view)

    indicator.startAnimating()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    getRequests.getType1(tableView,spinner: indicator)

    ref.addTarget(self, action: #selector(Catalog1TableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
    ref.attributedTitle = NSAttributedString(string: "Загрузка")
    tableView.addSubview(ref)

}


func updateSearchResultsForSearchController(searchController: UISearchController)
{
    indicator.startAnimating()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    tableView.reloadData()
    getRequests.getProduct(tableView, spinner: indicator,name: searchController.searchBar.text!)
    for item in getRequests.product {
        if item.productTitle.containsString(searchController.searchBar.text!){
            filteredTableData.append(item)
        }

    }
    tableView.reloadData()
}
like image 686
beka.angsabay Avatar asked Jul 11 '16 06:07

beka.angsabay


2 Answers

Try to implement UISearchBar Delegate method :

func searchBarSearchButtonClicked(searchBar: UISearchBar)
like image 153
Abd Aboudi Avatar answered Sep 19 '22 07:09

Abd Aboudi


You should implement something like this in your view controller.

var resultSearchController = UISearchController()
resultSearchController.searchBar.delegate = self

extension ViewController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        print("search button click")
    } 
}
like image 28
shrinidhi kulkarni Avatar answered Sep 21 '22 07:09

shrinidhi kulkarni