如何在 swift 中向 uitableview 添加搜索栏?
How to add a searchbar to tableview in swift?
我正在尝试添加一个搜索栏功能来实际过滤我的数据。我已经能够生成我需要的数据并将其填充到表视图中。但是,当我使用以下代码进行搜索时,搜索变量总是返回 false,因此 tableview 永远不会更新。
class MainTableViewController: UITableViewController {
let searchBarController = UISearchController(searchResultsController: nil)
let listData = ["Sam", "Jam", "Tam", "Pam", "Kam", "Nam", "Mam"]
var filteredData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Demo"
setSearchBarUI()
getFilteredData()
}
func setSearchBarUI() {
searchBarController.searchBar.delegate = self
searchBarController.obscuresBackgroundDuringPresentation = false
searchBarController.searchBar.sizeToFit()
navigationItem.searchController = searchBarController
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchStuff", for: indexPath) as! SearchStuff
cell.titleLabel.text = filteredData[indexPath.row]
return cell
}
func getFilteredData(searchedText: String = String()) {
let filteredListData: [String] = listData.filter({ (object) -> Bool in
searchedText.isEmpty ? true : object.lowercased().contains(searchedText.lowercased())
})
filteredData = filteredListData
tableView.reloadData()
}
}
extension MainTableViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
getFilteredData(searchedText: searchBar.text ?? String())
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
searchBar.text = String()
getFilteredData()
}
}
输出:
这样您就可以忽略使用 searching
变量来处理 table 视图的不同状态。
如果您正在使用 UINavigationController
,您可以轻松实现 UISearchController
let searchController = UISearchController(searchResultsController: nil)
private func setSearchController() {
searchController.searchBar.placeholder = "Search".localized
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setSearchController()
}
我正在尝试添加一个搜索栏功能来实际过滤我的数据。我已经能够生成我需要的数据并将其填充到表视图中。但是,当我使用以下代码进行搜索时,搜索变量总是返回 false,因此 tableview 永远不会更新。
class MainTableViewController: UITableViewController {
let searchBarController = UISearchController(searchResultsController: nil)
let listData = ["Sam", "Jam", "Tam", "Pam", "Kam", "Nam", "Mam"]
var filteredData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Demo"
setSearchBarUI()
getFilteredData()
}
func setSearchBarUI() {
searchBarController.searchBar.delegate = self
searchBarController.obscuresBackgroundDuringPresentation = false
searchBarController.searchBar.sizeToFit()
navigationItem.searchController = searchBarController
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchStuff", for: indexPath) as! SearchStuff
cell.titleLabel.text = filteredData[indexPath.row]
return cell
}
func getFilteredData(searchedText: String = String()) {
let filteredListData: [String] = listData.filter({ (object) -> Bool in
searchedText.isEmpty ? true : object.lowercased().contains(searchedText.lowercased())
})
filteredData = filteredListData
tableView.reloadData()
}
}
extension MainTableViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
getFilteredData(searchedText: searchBar.text ?? String())
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
searchBar.text = String()
getFilteredData()
}
}
输出:
这样您就可以忽略使用 searching
变量来处理 table 视图的不同状态。
如果您正在使用 UINavigationController
,您可以轻松实现 UISearchController
let searchController = UISearchController(searchResultsController: nil)
private func setSearchController() {
searchController.searchBar.placeholder = "Search".localized
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setSearchController()
}