因此,我制作了一个iOS应用,并从firebase实时数据库中获取了位置,并通过异步将位置加载到了表格视图中,并且我需要搜索栏将位置名称与用户选择的触摸正确匹配。问题在于,在所有异步下载之后,搜索正确地将关键字与位置名称匹配,但是当用户单击它时,它实际上打开了一个不同的位置,就像原始表格视图在底部一样,然后搜索结果是显示在原始图片的顶部,当用户单击该图层时,它实际上会打开该图层下方的原始位置
我不知道为什么,也许我需要为搜索创建第二个表视图,但是我不明白为什么它会在搜索结果层的下面打开。
searchCell
:
import Foundation
import UIKit
class searchCell: UITableViewCell {
@IBOutlet weak var nameTextLabel: UILabel!
@IBOutlet weak var locationTextLabel: UILabel!
func setData(data: NightClubs) {
nameTextLabel.text = data.name
locationTextLabel.text = data.location
}
}
MasterViewController
:
import Foundation
import UIKit
import FirebaseDatabase
class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
@IBOutlet var searchFooter: SearchFooter!
let searchController = UISearchController(searchResultsController: nil)
var detailViewController: DetailViewController? = nil
var filteredNightClubs = [NightClubs]()
var nightClubs = [NightClubs]()
var smallerNightClubs = [NightClubs]()
var tempImage = #imageLiteral(resourceName: "Your Club Here")
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
dataService()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search NightClubs"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["All", "Barrie", "Toronto", "London"]
searchController.searchBar.delegate = self
tableView.tableFooterView = searchFooter
}
override func viewWillAppear(_ animated: Bool) {
if let selectionIndexPath = self.tableView.indexPathForSelectedRow {
self.tableView.deselectRow(at: selectionIndexPath, animated: animated)
}
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func dataService() {
DataService.ds.REF_BARS.observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot.value as Any)
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
print(snap)
if let barData = snap.value as? Dictionary<String, AnyObject> {
let bar = NightClubs(barData: barData)
self.nightClubs.append(bar)
print(self.nightClubs)
self.tableView.reloadData()
}
self.tableView.reloadData()
}
self.tableView.reloadData()
self.smallerNightClubs = self.nightClubs//.filter { $0.promoted == "Yes"}
}
self.tableView.reloadData()
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
searchFooter.setIsFilteringToShow(filteredItemCount: filteredNightClubs.count, of: smallerNightClubs.count) // nightClubs.count
return filteredNightClubs.count
}
searchFooter.setNotFiltering()
return nightClubs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell") as! searchCell
let nightClub: NightClubs
if isFiltering() {
nightClub = filteredNightClubs[indexPath.row]
} else {
nightClub = nightClubs[indexPath.row]
}
cell.locationTextLabel.text = nightClub.location
cell.nameTextLabel.text = nightClub.name
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is DetailViewController
{
if let indexPath = self.tableView.indexPathForSelectedRow {
let vc = segue.destination as? DetailViewController
vc?.barName = nightClubs[indexPath.row].name
vc?.barLocation = nightClubs[indexPath.row].location
vc?.barImage = UIImage(named: nightClubs[indexPath.row].name) ?? tempImage
vc?.barPopulation = nightClubs[indexPath.row].goingCount
vc?.barDescription = nightClubs[indexPath.row].description
vc?.barAddress = nightClubs[indexPath.row].address
}
} else {
print("failed segue")
}
}
func searchBarIsEmpty() -> Bool {
return searchController.searchBar.text?.isEmpty ?? true
}
func filterContentForSearchData(_ searchText: String, scope: String = "All") {
filteredNightClubs = smallerNightClubs.filter({( place : NightClubs) -> Bool in
let doesCategoryMatch = (scope == "All") || (place.location == scope)
if searchBarIsEmpty() {
return doesCategoryMatch
} else {
return doesCategoryMatch && place.name.lowercased().contains(searchText.lowercased())
}
})
tableView.reloadData()
}
func isFiltering() -> Bool {
let searchBarScopeIsFiltering = searchController.searchBar.selectedScopeButtonIndex != 0
return searchController.isActive && (!searchBarIsEmpty() || searchBarScopeIsFiltering)
}
}
extension MasterViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchData(searchController.searchBar.text!, scope: scope) //Text
}
}
extension MasterViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchData(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) //Text
}
}