2016-07-21 17 views
2

私はinputviewにtableviewを表示し、選択時に値を返します。検索バーをtableviewに追加しましたが、問題は、searchbarテーブルビューをタップすると自動的に閉じて再オープンすることです。カントも検索バーをクリックします。何かアドバイス?検索バーを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に言及して以来。テーブルビューが表示されます。

検索バーをクリックすると、Now Searchバーが最初のレスポンダーになります。 TextFIeldはもはや最初のレスポンダーではないので、テーブルの表示が消える。

解決策の1つは次のとおりです。 テーブルビューを入力ビューからテキストフィールドにしないでください。それは、次のとおりです。 //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) 
} 
関連する問題