• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Swift – Filter Bar Tutorial

Home ios Swift – Filter Bar Tutorial

Swift – Filter Bar Tutorial

Mar 9, 2017 | Posted by Andrew | ios, swift, tutorial, xcode |

In our previous tutorial we implemented a search bar for a table with information about cupcakes. In this tutorial we will extend the app to include a filter bar as below. You can download the starting source code for this tutorial here.

First of all add UISearchBarDelegate to our view controller

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate {

Then in view did load ad the following code. It will setup our filter bar and set the values for it which is All, Small, Medium and finally Large.

        searchController.searchBar.scopeButtonTitles = ["All", "Small", "Medium", "Large"]
        searchController.searchBar.delegate = self

The we need to add the following new function which is going to apply the search for both the search box text and the filter bar selection:

    func applySearch(searchText: String, scope: String = "All") {
        // If we haven't typed anything into the search bar then do not filter the results
        if searchController.searchBar.text! == "" {
            filteredCakes = cakes.filter { cake in
                let cakeSize = (scope == "All") || (cake.size == scope)
                return cakeSize
            }
        } else {
            // Filter the results based on the selected filer and search text
            filteredCakes = cakes.filter { cake in
                let cakeSize = (scope == "All") || (cake.size == scope)
                return cakeSize && cake.name.lowercased().contains(searchText.lowercased())
            }
        }
        
        self.tableView.reloadData()
    }

It first checks if the search bar is empty, if so it will only apply the selected cupcake size in the filter bar. Otherwise it will only show cakes based on the search bar and the filter bar size.

Next modify the updateSearchResults function as below, and also add the searchBar selectedScopeButtonIndexDidChange function. These are called whenever you type text in the search bar or select a cupcake size from the filter bar.

    func updateSearchResults(for searchController: UISearchController) {
        //applySearch(searchText: searchController.searchBar.text!)
        let searchBar = searchController.searchBar
        let selectedScope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
        applySearch(searchText: searchController.searchBar.text!,scope: selectedScope)
    }
    
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        applySearch(searchText: searchController.searchBar.text!,scope: searchBar.scopeButtonTitles![selectedScope])
    }

Now you can run the app and see the filter bar working. You can either

  • Select a cupcake size from the filter bar
  • Type in the cake name you wish to search for
  • Or use a combination of both

Good work! Have this E-Cupcake as a reward!

download source code

Tags: filterfilter barfindsearchsearch bartable
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Top 5 tips for XCode Productivity

Jun 3, 2017

XCode has a wide variety of shortcut and handy features[...]

Swift – Search Bar Tutorial

Mar 5, 2017

In this tutorial we are going to implement a search[...]

Swift Algorithms – Binary Search

Feb 8, 2017

The binary search is the one of the fastest searches[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2026 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next