0

I have implemented a simple UISearchController into my UITableViewController programatically. And below is my complete code:

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

    let appleProducts = ["Mac","iPhone","Apple Watch","iPad"]
    var filteredAppleProducts = [String]()
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()

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

        self.tableView.tableHeaderView = self.resultSearchController.searchBar

        self.tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if (self.resultSearchController.active){
            return self.filteredAppleProducts.count
        }else{
            return self.appleProducts.count
        }
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell?

        if (self.resultSearchController.active)
        {
            cell!.textLabel?.text = self.filteredAppleProducts[indexPath.row]

            return cell!
        }
        else
        {
            cell!.textLabel?.text = self.appleProducts[indexPath.row]

            return cell!
        }
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let vc = self.storyboard!.instantiateViewControllerWithIdentifier("Detail") as! DetailViewController
        self.navigationController!.pushViewController(vc, animated: true)
    }
    func updateSearchResultsForSearchController(searchController: UISearchController){

        self.filteredAppleProducts.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let array = (self.appleProducts as NSArray).filteredArrayUsingPredicate(searchPredicate)
        self.filteredAppleProducts = array as! [String]

        self.tableView.reloadData()
    }

}

My resultSearchController is working completely fine but when I click on any cell then UISearchController is appearing into my next view too as shown into below image:

enter image description here

But my Second view is empty.

how I can remove this resultSearchController when I switch to another view so it will not appear into next view?

Project Sample for more Info.

2

1 Answer 1

2

Just add one line in viewDidLoad

 self.definesPresentationContext = true

Doc

A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

GIF

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.