【问题标题】:UITableView with Search bar as inputView带有搜索栏的 UITableView 作为 inputView
【发布时间】:2016-07-21 21:30:15
【问题描述】:

我有一个文本字段,它在其 inputview 上显示一个 tableview 并在选择时返回值我向 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

}

【问题讨论】:

    标签: ios swift uitableview uisearchbar


    【解决方案1】:

    单击 Text field 后,您的文本字段将成为第一响应者。既然你提到了 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多