在推送到详细信息视图后重新调整搜索栏

时间:2016-03-12 00:56:06

标签: swift

我想知道您是否可以在推送到详细视图后退出搜索栏,因为每当我点击详细视图时,我总是要在搜索栏上按取消。

这是我的搜索代码。

import UIKit

class TableViewLemon: UITableViewController, UISearchResultsUpdating {



let tableData = ["Apple","Google","Facebook","Amazon","Exxon Mobil"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()


override func viewDidLoad() {
    super.viewDidLoad()

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

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    // Reload the table
    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 {
    // 1
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // 2
    if (self.resultSearchController.active) {
        return self.filteredTableData.count
    }
    else {
        return self.tableData.count
    }
}


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

    // 3
    if (self.resultSearchController.active) {
        cell.textLabel?.text = filteredTableData[indexPath.row]

        return cell
    }
    else {
        cell.textLabel?.text = tableData[indexPath.row]

        return cell
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    if (segue.identifier == "DetailView") {
        let VC = segue.destinationViewController as! DetailViewLemon
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let Make = self.resultSearchController.active ?
                filteredTableData[indexPath.row] as String  :
                tableData[indexPath.row] as String
            VC.sentData1 = Make
        }
    }
}








func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filteredTableData.removeAll(keepCapacity: false)

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

    self.tableView.reloadData()
}
}

1 个答案:

答案 0 :(得分:0)

只需在准备segue中添加resignFirstResponder()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "DetailView") {
        let VC = segue.destinationViewController as! DetailViewLemon
        searchBar.resignFirstResponder()
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let Make = self.resultSearchController.active ?
                filteredTableData[indexPath.row] as String  :
                tableData[indexPath.row] as String
            VC.sentData1 = Make
        }
    }
}