【发布时间】:2017-08-04 12:46:20
【问题描述】:
我遇到了许多其他用户在 StackOverflow 上提出的问题(例如:1、2、3)。
我希望在滚动表格视图时有一个固定搜索栏。我最初有一个UITableViewController,顶部有一个UISearchBar。
大多数解决方案建议我应该有一个UIViewController,下面有一个UISearchBar 和一个UITableView。我正是这样做的。下面是我的界面生成器的截图。
这个解决方案并没有解决它,但是 :( 搜索栏仍然不浮动,滚动时它会隐藏在导航栏后面。
我也尝试过this solution,但我宁愿在导航栏中没有搜索栏。
这是我所有的 TableView 或 Search 相关代码(我删除了所有不相关的内容):
class NumberSelectorViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tableView: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// NOTE: Removing thie line removes the search bar entirely.
tableView.tableHeaderView = searchController.searchBar
addCancelButton()
}
// MARK: - Table view data source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching() == true {
return filteredNumberList.count
}
return NumberList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell")! as UITableViewCell
if isSearching() == true {
cell.textLabel?.text = filteredNumberList[indexPath.row].NumberName
} else {
cell.textLabel?.text = NumberList[indexPath.row].NumberName
}
return cell
}
// MARK: - Table view delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if isSearching() == true {
selectedNumber = filteredNumberList[indexPath.row]
} else {
selectedNumber = NumberList[indexPath.row]
}
performSegue(withIdentifier: "someSegue", sender: self)
}
// MARK: Searching
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredNumberList = NumberList.filter({ (Number) -> Bool in
return (Number.NumberName?.lowercased().contains(searchText.lowercased()))!
})
tableView.reloadData()
}
// MARK: Search Bar
func isSearching() -> Bool {
return (searchController.isActive && searchController.searchBar.text != "")
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredNumbersList.removeAll()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.tableView.reloadData()
}
extension NumberSelectorViewController: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
}
【问题讨论】:
-
不要将搜索栏添加为表头,这就是它浮动的原因 - 在您的视图控制器中将其添加为
self.view的子视图,或者在界面生成器中执行此操作 -
@mag_zbc 抱歉,这可能是一个愚蠢的问题,但它不是我在界面构建器中的视图控制器的子视图(参见我帖子中的屏幕截图)吗?
标签: ios swift uitableview tableview uisearchbar