ios - 带有搜索栏的 UITableView 作为 inputView
<p><p>我有一个文本字段,它在其 inputview 上显示一个 tableview 并在选择时返回值我向 tableview 添加了一个搜索栏,但问题是,当我点击搜索栏时,tableview 会自动关闭和重新打开,这意味着我什至无法单击搜索栏。有什么建议吗?</p>
<p>这里是代码。</p>
<pre><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 =
tableView.tableHeaderView = navigationBar
toTextField.inputView = self.tableView
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>单击 Text field 后,您的文本字段将成为第一响应者。既然你提到了 toTextField.inputView = self.tableView;这样你的表格 View 就会出现。</p>
<p>点击搜索栏后,立即搜索栏即为第一响应者。由于 TextFIeld 不再是第一响应者,因此表格 View 消失。</p>
<p>一种解决方案可以是:
不要将表格 View 作为文本字段的输入 View 。那是 :
//toTextField.inputView = self.tableView</p>
<p>相反,您可以使用 TextField 的委托(delegate)方法之一呈现表格 ViewController :</p>
<pre><code> 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)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 带有搜索栏的 UITableView 作为 inputView,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38514694/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38514694/
</a>
</p>
页:
[1]