我正試圖添加一個搜索欄功能來實際過濾我的資料。我已經能夠生成我需要的資料并將其填充到一個表視圖中。然而,當我使用以下代碼進行搜索時,搜索變數總是回傳為false,因此表視圖從未被更新。
// MARK: - Search Bar
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
搜索 = true
searchstuff = stuff.filter({$0.lowercased().prefix(searchText.count) = searchText.lowercased()})
print( searching)
self.tableView.reloadData()
}
// Cancel button for when one is searching[/span]。
func searchBarCancelButtonClicked(_ searchBar。UISearchBar) {
搜索 = false {
self.searchBar.endEditing(true)
self.tableView.reloadData()
}
//MARK: - 鍵盤解散。
override func scrollViewWillBeginDragging(_ scrollView。UIScrollView) {
self.searchBar.endEditing(true)
}
//確保在點擊其他地方時退出搜索欄。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.endEditing(true)
}
//MARK: - 表視圖資料源。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.backgroundColor = .black
if 搜索 == true {
return searchstuff.count
}
else {
return stuff.count
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let stuffProfileVC = searchstuff(collectionViewLayout: UICollectionViewLayout()
navigationController?.pushViewController(stuffProfileVC, animated: true)
標題 = searchstuff[indexPath.row] 。
}
//這是每個單獨的東西名稱的表視圖。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SearchStuff
cell.textLabel?.textColor=.white
cell.backgroundColor = .black
print(搜索)
if searching == true {
cell.textLabel?.text = searchstuff[indexPath.row] ?
}
else {
cell.textLabel?.text = stuff[indexPath.row]
}
return cell
}
uj5u.com熱心網友回復:
如果你使用的是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()
}
uj5u.com熱心網友回復:
class MainTableViewController。UITableViewController {
let searchBarController = UISearchController(searchResultsController: nil)
let listData = ["Sam"/span>, "Jam"/span>, "Tam"/span>, "Pam"/span>, "Kam"/span>, "Nam"/span>, "Mam"/span>]
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變數來處理表視圖的不同狀態。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311741.html
標籤:
