Search code examples
iosswiftuitableviewuisearchcontroller

How can I update my Table Controller filtering with a Search Controller?


I'm new to IOS development, so it's being hard to understand a few things.

In my app, when the users open the main screen, I'll show a list of tasks and a search to filter the tasks. If the user type "Monday" in my search controller, It should filter only the tasks from Mondays. All the code is working fine, but I don't know if it's the right way.

I have a Table View Controller which has all the tasks and shows when the user opens the app. So, the first thing the user should see is all his tasks.

When the user opens the search in the table header, it should filter his tasks (from the first table) and show only the tasks corresponding to the filter. It is working ok too.

Now, my problem. I have two tables. One for the main view controller and other to the search controller. I have to code twice times the get cell and editing style for delete tasks.

What am I doing wrong? How can I have a filter to my main table and keep everything in the main table, instead of copying everything to the second controller?

Thanks


Solution

  • First of all no need to use two tables for search and display, its not a good approach at all. Also you should look for for apple documentation on Search Controller. You should use Search Bar and Search Display Controller and implement the following code in its viewDidLoad method

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
    

    Following method filters your main array based based on searchText and will put the results in the filteredData array.

    func filterContentForSearchText(searchText: String, scope: String = "All") {
         filteredData = array.filter { string in
         return     
         string.lowercaseString.containsString(searchText.lowercaseString)
      }
    
       tableView.reloadData()
    }
    

    To allow ViewController to respond to the search bar, it will have to implement UISearchResultsUpdating. Open ViewController.swift and add the following class extension, outside of the main ViewController class:

    extension MasterViewController: UISearchResultsUpdating {
      func updateSearchResultsForSearchController(searchController: UISearchController) {
        filterContentForSearchText(searchController.searchBar.text!)
      }
    }
    

    Now in your numberOfRowsInSection and cellForRowAtIndexPath method do the following

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          if searchController.active && searchController.searchBar.text != "" {
        return filteredData.count
      }
      return array.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
      let string: String
      if searchController.active && searchController.searchBar.text != "" {
    string = filteredData[indexPath.row]
      } else {
          string = array[indexPath.row]
       }
      cell.textLabel?.text = string
      return cell
    }
    

    And for more details you can go to the following website. https://www.raywenderlich.com/113772/uisearchcontroller-tutorial