2016-07-21 22 views
2

Ich habe Textfield, die eine Tabellenansicht auf seine Eingabeansicht zeigt und gibt Wert auf Select Ich habe eine Suchleiste zu Tableview hinzugefügt, aber das Problem ist, wenn ich auf searchbar tableview automatisch schließen und öffnen, was bedeutet, dass ich kann nicht einmal auf die Suchleiste klicken. Irgendein Rat?UITableView mit Suchleiste als EingabeView

Hier ist der Code.

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 

} 

Antwort

0

Sobald Sie auf Textfeld klicken, wird Ihr Textfeld zum Ersthelfer. Da du toTextField.inputView = self.tableView erwähnt hast; So erscheint die Tabellenansicht.

Sobald Sie auf die Suchleiste klicken, wird die Suchleiste jetzt als Ersthelfer angezeigt. Da TextFIld nicht mehr Erstantragsteller ist, verschwindet die Tabelle View Disappear.

Eine Lösung kann sein: Tabellenansicht nicht als Eingabeansicht für Textfeld machen. das heißt: //toTextField.inputView = self.tableView

Stattdessen können Sie Tabelle View-Controller mit einer der Delegatmethode von TextField- präsentieren:

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) 
}