【发布时间】:2017-09-30 16:26:41
【问题描述】:
搜索栏的新手。
工作:
[String] "firstname" 在搜索时返回正确的值。如果我有 3 个“名字”以“G”开头的人(例如)表格会重新加载 3 个单元格。
问题:
虽然表格重新加载了“名字”的正确单元格值,但 users.append(user) 返回 nil 并且错误的名称被加载到表格视图中。
协助:
搜索完成后如何将正确的名称加载到 tableview?
这是我的代码:
func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String)->Void {
FIRDatabase.database().reference().child("users").queryOrdered(byChild: "firstname").queryStarting(atValue: textSearched).queryEnding(atValue: textSearched+"\u{f8ff}").observe(.value, with: { snapshot in
var users = [User]()
let user = User()
print(user)
for _ in snapshot.children.allObjects as! [FIRDataSnapshot] {
if let dictionary = snapshot.value as? [String: AnyObject]{
user.lastname = dictionary["firstname"] as? String
users.append(user)
}
}
self.users = users
let search = searchCell()
search.firstName.text = user.firstname
self.attempReloadOfTable()
}, withCancel: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! searchCell
var user = User()
user = users[indexPath.row]
if let id = user.id{
let ref = FIRDatabase.database().reference().child("users").child(id)
ref.observe(.value, with: { (snapshot) in
cell.lastName.text = user.lastname
cell.firstName.text = user.firstname
})
}
return cell
}
【问题讨论】:
标签: ios firebase swift3 firebase-realtime-database swift2