Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISearchController将与swift一起出现在下一个视图中_Ios_Swift_Uinavigationbar_Uisearchcontroller - Fatal编程技术网

Ios UISearchController将与swift一起出现在下一个视图中

Ios UISearchController将与swift一起出现在下一个视图中,ios,swift,uinavigationbar,uisearchcontroller,Ios,Swift,Uinavigationbar,Uisearchcontroller,我以编程方式将一个简单的UISearchController实现到我的UITableViewController中。下面是我的完整代码: import UIKit class TableViewController: UITableViewController, UISearchResultsUpdating { let appleProducts = ["Mac","iPhone","Apple Watch","iPad"] var filteredAppleProduct

我以编程方式将一个简单的
UISearchController
实现到我的
UITableViewController
中。下面是我的完整代码:

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
也会出现在我的下一个视图中,如下图所示:

但我的第二个观点是空的

如何在切换到另一个视图时删除此
resultSearchController
,使其不会出现在下一个视图中


项目以获取更多信息。

只需在
viewDidLoad

 self.definesPresentationContext = true
医生

一个布尔值,指示当视图控制器或其子体之一显示视图控制器时,是否覆盖此视图控制器的视图

GIF


查看此链接我的朋友谢谢您的链接@安布.卡提克