【发布时间】:2018-10-12 22:09:47
【问题描述】:
我有一个带有 SearchBar 的 TableView,但是当我滚动列表时,顶部栏的搜索栏消失,我需要向上滚动以显示搜索栏。我在谷歌和堆栈溢出尝试了几种解决方案,但没有解决我的问题。
我试过这个:
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
conteudo = ["Augusto","Luis","Joao","Marina","Taina","Evandra","Miguel","Daniela","Luciano","Patricia","Jose","Vinicius","John","William"]
currentConteudo = conteudo
currentConteudo.sort()
tableView.dataSource = self
tableView.delegate = self
tableView.tableHeaderView = searchBar
tableView.estimatedSectionHeaderHeight = 50
searchBar.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentConteudo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellSearchCustomer")!
cell.textLabel?.text = currentConteudo[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return searchBar
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
滚动:
未滚动:
故事板:
【问题讨论】:
-
您只想让
searchBar一直可见吗? -
是的,它是.....
-
@Augusto,你想在 tableview 向上滚动时滚动搜索栏。?或者,当表格视图向上滚动时,您希望搜索栏不滚动。?
-
我希望搜索栏永远不会丢失。向上或向下滚动,搜索栏必须始终可见。固定在顶部
标签: ios swift xcode uitableview uisearchbar