Ios 以编程方式将UISearchController添加到swift 2中的UITableView标头

Ios 以编程方式将UISearchController添加到swift 2中的UITableView标头,ios,uitableview,swift2,uisearchcontroller,Ios,Uitableview,Swift2,Uisearchcontroller,在以前版本的my view controller(基于故事板)中,搜索栏工作正常。 在100%编程定义的当前版本中(我删除了所有故事板引用),搜索栏消失了。 我试图理解问题所在,但没有成功。有什么想法吗 以下是错误的代码片段: let resultSearchController = UISearchController() override public func viewDidLoad() { super.viewDidLoad() // No nav bar n

在以前版本的my view controller(基于故事板)中,搜索栏工作正常。 在100%编程定义的当前版本中(我删除了所有故事板引用),搜索栏消失了。 我试图理解问题所在,但没有成功。有什么想法吗

以下是错误的代码片段:

let resultSearchController = UISearchController()

override public func viewDidLoad() {
    super.viewDidLoad()

    // No nav bar
    navigationController?.navigationBar.hidden = true

    // — Add table view
    view.addSubview(tableView)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.leftAnchor.constraintEqualToAnchor(view.leftAnchor, constant: 0).active = true
    tableView.rightAnchor.constraintEqualToAnchor(view.rightAnchor, constant: 0).active = true
    tableView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor, constant: 0).active = true


    // Add search bar as a the table header

    resultSearchController.dimsBackgroundDuringPresentation = false
    resultSearchController.definesPresentationContext = true
    resultSearchController.hidesNavigationBarDuringPresentation = false

    resultSearchController.searchResultsUpdater = self
    resultSearchController.searchBar.sizeToFit()
    resultSearchController.searchBar.delegate = self
    tableView.tableHeaderView = resultSearchController.searchBar

    /// Add refresh control
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(BigHeadViewController.refreshTable), forControlEvents: UIControlEvents.ValueChanged)
    tableView.addSubview(refreshControl)  
}

我想问题可能在第一行。您必须指定“searchResultsController”作为初始化的一部分-即使它为零(就地搜索)。试一试

let resultSearchController = UISearchController(searchResultsController:nil)