Ios UITableView,搜索栏为inputView

Ios UITableView,搜索栏为inputView,ios,swift,uitableview,uisearchbar,Ios,Swift,Uitableview,Uisearchbar,我有一个textfield,它在inputview上显示一个tableview,在select上返回值。我在tableview上添加了一个搜索栏,但问题是,当我点击搜索栏tableview时,它会自动关闭并重新打开,这意味着我甚至不能点击搜索栏。有什么建议吗 这是代码 class myClass: UIViewController, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate, UISearchBarDe

我有一个textfield,它在inputview上显示一个tableview,在select上返回值。我在tableview上添加了一个搜索栏,但问题是,当我点击搜索栏tableview时,它会自动关闭并重新打开,这意味着我甚至不能点击搜索栏。有什么建议吗

这是代码

class myClass: UIViewController, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate, UISearchBarDelegate {

    //the all are in viewdidload
    var tableView: UITableView  =   UITableView()
    tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
    let navigationItem = UINavigationItem()
    var searchBar:UISearchBar = UISearchBar()
    searchBar.searchBarStyle = UISearchBarStyle.Prominent
    searchBar.placeholder = " Search..."
    searchBar.sizeToFit()
    searchBar.translucent = false
    searchBar.backgroundImage = UIImage()
    searchBar.delegate = self
    navigationBar.items = [navigationItem]

    tableView.tableHeaderView = navigationBar

    toTextField.inputView = self.tableView

}

单击文本字段后,您的文本字段将成为第一响应者。因为您提到了toTextField.inputView=self.tableView;这样就出现了表视图

只要你们点击搜索栏,现在搜索栏就是第一个响应者。由于TextFIeld不再是第一响应者,因此表视图将消失

一种解决方案可以是: 不要将表视图作为文本字段的输入视图。即: //toTextField.inputView=self.tableView

相反,您可以使用TextField的委托方法之一显示表视图控制器:

 func textFieldDidBeginEditing(textField: UITextField)   {
    let test : UITableViewController = UITableViewController.init(style: UITableViewStyle.Plain)
    test.tableView = self.tableView
    let testNav: UINavigationController = UINavigationController(rootViewController: test);
    self.presentViewController(testNav, animated: true, completion: nil)
}