如何在Xcode 11中创建一个连接到Firebase的搜索栏页面



我正试图在我的应用程序中实现一个带有表视图的搜索栏,用于搜索Firebase存储中的数据。我在做这个的时候犯了很多错误。我的两个错误有Use of unresolved identifier 'cell',两个有Use of unresolved identifier 'inSearchMode',接下来的两个是Value of type 'Storage' has no subscriptsValue of type 'Storage' has no member 'filter'。我试着找出这些错误已经有一段时间了。如有任何帮助,我们将不胜感激!非常感谢。

ps错误显示为注释:

import Foundation

导入Firebase

类SearchBarViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var data = Storage.storage()
var filteredData = [String]()
var isSearching = false
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredData.count
}
return data.accessibilityElementCount()  //Might be a problem
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) is DataCell {
let text: String!
if isSearching {
text = filteredData[indexPath.row]
} else {
text = data[indexPath.row] //Value of type 'Storage' has no subscripts
}
cell.configureCell(data: text) //Use of unresolved identifier 'cell'
return cell //Use of unresolved identifier 'cell'

} else {
return UITableViewCell()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false //Use of unresolved identifier 'inSearchMode'
view.endEditing(true)
tableView.reloadData()
} else {
inSearchMode = true //Use of unresolved identifier 'inSearchMode'
filteredData = data.filter({$0 == searchBar.text!}) //Value of type 'Storage' has no member 'filter'
tableView.reloadData()
}
}

}

由于您实际上没有创建任何单元格而导致的错误Use of unresolved identifier 'cell',您只需键入checkis DataCell。为了解决这个问题,这条线应该是

if let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as? DataCell { 
}

对于第二个问题,访问data[indexPath.row],因为我不知道它是什么数据类型,所以不能给你答案。

对于第三个问题Use of unresolved identifier 'inSearchMode',没有为其声明变量。isSearching可能是你应该用它来代替的

最新更新