使用UISearchController搜索包含Firebase数据的UITableView较慢/慢,表中的单元格更多。我尝试观察单个事件,而不是连续观察数据库,但这并没有任何改变。
我在这里遵循了Ray Wenderlich的指南:https://www.raywenderlich.com/472-uisearchcontroller-tutorial-getting-started
下载的示例代码不会滞后。我不知道为什么我的这么慢。例如,点击搜索栏需要1-2秒才能加载。
这是我的一些代码: 搜索控制器:
extension SubCategoryViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController){
filterContentForSearchText(searchController.searchBar.text!)
}
}
override func viewDidLoad() {
super.viewDidLoad()
searchBarSetup()
....
}
let searchController = UISearchController(searchResultsController: nil)
var filtered = [SubCategoryCellInfo]()
func searchBarIsEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? true
}
func filterContentForSearchText(_ searchText: String) {
filtered = subCategories.filter({( cellInfo : SubCategoryCellInfo) -> Bool in
return cellInfo.itemBrand!.lowercased().contains(searchText.lowercased())
})
subCategoryTable.reloadData()
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
func searchBarSetup() {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
}