Implementing UISearchController in UITableView with Swift - iOS

Implementing UISearchController in UITableView with Swift in iOS is a great way to add search functionality to your app. With UISearchController, you can easily add a search bar to your UITableView and allow users to search for content within the table.

├Źndice
  1. Step 1: Create a UISearchController
  2. Step 2: Implement UISearchResultsUpdating
  3. Step 3: Set the Table View Data Source

Step 1: Create a UISearchController

The first step in implementing UISearchController is to create an instance of it. You can do this in the viewDidLoad method of your view controller:

let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Items"
navigationItem.searchController = searchController
definesPresentationContext = true

In this example, we create an instance of UISearchController with no search results controller, set the searchResultsUpdater to self (which means that this view controller will handle the search results), and set the obscuresBackgroundDuringPresentation property to false (which means that the search bar will not obscure the background when it is active).

We also set the placeholder text for the search bar and assign the search controller to the navigationItem.searchController property. Finally, we set definesPresentationContext to true, which ensures that the search bar will be presented within the current view controller.

Step 2: Implement UISearchResultsUpdating

The next step is to implement the UISearchResultsUpdating protocol. This protocol requires you to implement a single method: updateSearchResults(for:). This method is called whenever the user types or deletes text in the search bar.

extension ViewController: UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        let searchString = searchController.searchBar.text
        // Filter the data array and update the table view
    }
}

In this example, we implement updateSearchResults(for:) to retrieve the current search string from the search bar and filter our data array accordingly. You will need to replace the comment with your own code to filter the data.

Step 3: Set the Table View Data Source

The final step is to set the table view data source. In this example, we assume that you already have a data source for your table view, and that it is an array of items:

var items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.isActive {
        // Return the number of items that match the search string
    } else {
        return items.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    if searchController.isActive {
        // Configure the cell with the matching item
    } else {
        // Configure the cell with the normal item
    }
    return cell
}

In numberOfRowsInSection, we check whether the search controller is active. If it is, we return the number of items that match the search string. Otherwise, we return the total number of items.

In cellForRowAt, we again check whether the search controller is active. If it is, we configure the cell with the matching item. Otherwise, we configure the cell with the normal item.

That's it! With these three steps, you can easily add search functionality to your UITableView with Swift in iOS using UISearchController.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information