2016-07-21 47 views
2

我有文本字段顯示在其inputview上的tableview和返回值select我向tableview添加了一個搜索欄,但問題是,當我點擊searchbar tableview自動關閉和重新打開這意味着我甚至不能點擊搜索欄。有什麼建議?UITableView與搜索欄作爲inputView

這是代碼。

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 

} 

回答

0

一旦你點擊文本字段,你的文本字段就成爲第一響應者。既然你提到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) 
} 
: //toTextField.inputView = self.tableView

相反,可以通過使用的TextField的委託方法之一呈現表視圖控制器