2015-10-26 45 views
0

我以編程方式將UISearchController簡單地執行到我的UITableViewController中。以下是我的完整代碼:UISearchController以快捷方式顯示在nextView中

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() 
    } 

} 

resultSearchController工作完全正常,但當我點擊任一單元格,然後UISearchController正出現在我的下一個視圖也如入下圖:

enter image description here

但我的第二種觀點是空的。

當我切換到另一個視圖時,如何刪除此resultSearchController,以便它不會出現在下一個視圖中?

項目Sample瞭解更多信息。

+0

看到這個鏈接我的朋友http://programmingthomas.com/blog/2014/10/5/uisearchcontroller-and-definespresentationcontext –

+0

謝謝你的鏈接。 @ Anbu.Karthik –

回答

2

只需添加一個行viewDidLoad

self.definesPresentationContext = true 

文件

一個布爾值,表明該視圖控制器或它的後代之一提出了一個視圖控制器此視圖控制器的觀點是否被覆蓋。

GIF