【问题标题】:tableview not displaying firebase userstableview 不显示 firebase 用户
【发布时间】:2018-06-02 08:42:13
【问题描述】:

我的表格视图有问题,没有显示原型单元格。我已经正确命名了每个单元格和标识符。这是我当前的代码:(谢谢!)

import UIKit
import Firebase
import FirebaseDatabase
class UsersTableViewController: UITableViewController {



@IBOutlet var tableview: UITableView!

var ref: DatabaseReference!

var user = [User]()


override func viewDidLoad() {
    super.viewDidLoad()
    getusers()
    // Do any additional setup after loading the view.
}

func getusers() {
    let ref = Database.database().reference()

    ref.child("users").child(Auth.auth().currentUser!.uid).queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

        let users = snapshot.value as? [String : AnyObject] ?? [:]

        for (_, value) in users
        {
            if let uid =  users["uid"] as? String
            {
                if uid != Auth.auth().currentUser!.uid
                {
                    let showUser = User()

                    if let fullname = users["fullname"] as? String, let imagePath = users["urlImage"] as? String
                    {
                        showUser.fullname = fullname
                        showUser.imagePath = imagePath
                        showUser.userID = uid
                        self.user.append(showUser)
                    }

                }


            }

        }
        self.tableview.reloadData()
    })

    ref.removeAllObservers()
}

override func numberOfSections(in tableview: UITableView) -> Int {
    return 1
}

override func tableView(_ tableview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserCell

    cell.nameLabel.text = self.user[indexPath.row].fullname
    cell.UserID = self.user[indexPath.row].userID
    cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
    checkFollowing(indexPath: indexPath)

    return cell
}

override func tableView(_ tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
    return user.count
}


override func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {

    let uid = Auth.auth().currentUser!.uid
    let ref = Database.database().reference()
    let key = ref.child("users").childByAutoId().key

    var isFollower = false

    ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        if let following = snapshot.value as? [String : AnyObject] {
            for (ke, value) in following {
                if value as! String == self.user[indexPath.row].userID {
                    isFollower = true

                    ref.child("users").child(uid).child("following/\(ke)").removeValue()
                    ref.child("users").child(self.user[indexPath.row].userID).child("followers/\(ke)").removeValue()

                    self.tableview.cellForRow(at: indexPath)?.accessoryType = .none
                }
            }
        }
        if !isFollower {
            let following = ["following/\(key)" : self.user[indexPath.row].userID]
            let followers = ["followers/\(key)" : uid]

            ref.child("users").child(uid).updateChildValues(following)
            ref.child("users").child(self.user[indexPath.row].userID).updateChildValues(followers)

            self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
        }
    })
    ref.removeAllObservers()

}


func checkFollowing(indexPath: IndexPath) {
    let uid = Auth.auth().currentUser!.uid
    let ref = Database.database().reference()

    ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        if let following = snapshot.value as? [String : AnyObject] {
            for (_, value) in following {
                if value as! String == self.user[indexPath.row].userID {
                    self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
                }
            }
        }
    })
    ref.removeAllObservers()

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
 // MARK: - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */

}

我目前正在使用 Google 的 Firebase API 进行用户存储。在这段代码中,我试图将用户获取到我的表格视图,只有我的表格视图不显示任何信息。如果您能提供帮助,请提前感谢您!

【问题讨论】:

  • 这个self.user.append(showUser) 随时都会被叫到吗?你检查了吗?
  • @Oxthor 嗨!我是 swift 新手,我不完全理解这个问题。但是,我确实知道我将我的用户信息附加到我的用户数组中,它应该使用 user.count 添加行。

标签: swift uitableview firebase firebase-realtime-database


【解决方案1】:

删除 ref.removeAllObservers() 行。方法observeSingleEvent 在完成执行后自动移除观察者。所以在你的情况下 ref.removeAllObservers() 可以在完成调用之前删除它们,因此完成中的任何代码都不会执行。

【讨论】:

  • 对不起,谢谢你的建议。但是,我尝试了您的建议,但我的 tableview 仍然没有显示任何单元格。
猜你喜欢
  • 1970-01-01
  • 2019-10-08
  • 2018-04-07
  • 2020-03-15
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多