【问题标题】:FIRDatabase UISearchBar string for child孩子的 FIRDatabase UISearchBar 字符串
【发布时间】: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


    【解决方案1】:

    您的问题是单元格在与块中的用户数据绑定之前返回。因为FIRBase结果查询块中的代码会在return cell执行完之后再执行。

    我像这样编辑了你的代码:

    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]()
            for _ in snapshot.children.allObjects as! [FIRDataSnapshot] {
                if let dictionary = snapshot.value as? [String: AnyObject] {
                    let user = User()
                    user.lastname = dictionary["firstname"] as? String
                    print(user)
                    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
        let user = users[indexPath.row]
    
        cell.lastName.text = user.lastname
        cell.firstName.text = user.firstname
    
        return cell
    }
    

    希望它对你有用。

    【讨论】:

    • 好的修复了错误,但现在它没有在搜索栏中提取“名字”的值。说 0 并返回零个单元格。**if let dictionary = child.value as?[String: AnyObject] { let user = User()**
    • 好的,所以你应该将代码保留在textDidChange 函数上,只需更改cellForRowAt 函数即可。
    • 是的,仍然无法正常工作。只需要弄清楚如何将正确的用户加载到表中。代码中的其他所有内容都有效。
    猜你喜欢
    • 2023-03-28
    • 2013-04-02
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多