How to update search results when changing the scope button. Swift UISearchController

How to update search results when changing the scope button (after my click on the scope)? Search results were changed (with a new area) when re-entering!

searchControl - config import UIKit

class ProductTableView: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating { @IBOutlet weak var tableView: UITableView! var searchController: UISearchController! var friendsArray = [FriendItem]() var filteredFriends = [FriendItem]() override func viewDidLoad() { super.viewDidLoad() searchController = UISearchController(searchResultsController: nil) searchController.searchBar.sizeToFit() searchController.searchResultsUpdater = self searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.scopeButtonTitles = ["Title","SubTitle"] definesPresentationContext = true tableView.tableHeaderView = searchController.searchBar self.tableView.reloadData() } 

Refresh Func When I print NSLog text, type your text and area number. When I change the scope - nothing !!!

 func updateSearchResultsForSearchController(searchController: UISearchController) { let searchText = searchController.searchBar.text let scope = searchController.searchBar.selectedScopeButtonIndex NSLog("searchText - \(searchText)") NSLog("scope - \(scope)") filterContents(searchText, scope: scope) tableView.reloadData() } 

Filter function

 func filterContents(searchText: String, scope: Int) { self.filteredFriends = self.friendsArray.filter({( friend : FriendItem) -> Bool in var fieldToSearch: String? switch (scope){ case (0): fieldToSearch = friend.title case(1): fieldToSearch = friend.subtitle default: fieldToSearch = nil } var stringMatch = fieldToSearch!.lowercaseString.rangeOfString(searchText.lowercaseString) return stringMatch != nil }) } 

Help me please!

+6
source share
1 answer

The behavior that you expect is logical and seems right at first glance, but in fact it is not. Fortunately, there is an easy workaround.

Here is the description of the Apple method:

Called when the search bar becomes the first responder or when a user makes changes to the search bar.

Changing an area is a change in the search bar, right? It makes sense to me. But if you read the discussion , Apple makes it clear that the behavior is not what you expect:

This method is automatically called whenever the search bar becomes the first responder or we enter text into the search bar.

Not included: changes in the field. It’s strange not to notice, is it? Either the method should be called when the scope is changed, or it should be clear in the summary that this is not so.

You can get the behavior you searchController.searchBar.delegate adding the UISearchBarDelegate protocol to your view controller and setting searchController.searchBar.delegate to your view controller.

Then add the following:

 func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { updateSearchResultsForSearchController(searchController) } 

This will cause updateSearchResultsForSearchController to run when the scope changes as you expect. But instead, you may need to change the meat of updateSearchResultsForSearchController to a new method that calls both the updateSearchResultsForSearchController and selectedScopeButtonIndexDidChange calls.

+8
source

All Articles