Firebase中的数据未显示在UITableView中

时间:2019-07-08 20:05:00

标签: ios swift firebase uitableview

我有一个带有自定义单元格的表格视图,它是从firebase获取数据的。当我运行该应用程序时,表视图中没有任何显示。在我的第二个数据库参考工作之后,我的打印语句都没有,但是数据库参考是正确的。 这是我的数据库结构(我尽可能地简化了它):

{
   "Classes" : {
"-LiGRdN6aoXHfE9wn0TW" : {
  "Students" : {
    "J526G6DB3uONdNM2hzgDYdgokd93" : "J526G6DB3uONdNM2hzgDYdgokd93",
    "UhTQgJ4jugMqFtEZK8CsIkTqQl22" : "UhTQgJ4jugMqFtEZK8CsIkTqQl22"
  },
  "class_info" : {
    "date_clasname" : "mm",
    "room_number" : "jj",
    "subject" : "Science",
    "teacher" : "mm",
    "uid" : "-LiGRdN6aoXHfE9wn0TW"
  }
},
"-LiGRe3e1YF_HFrHKMgi" : {
  "Students" : {
    "J526G6DB3uONdNM2hzgDYdgokd93" : "J526G6DB3uONdNM2hzgDYdgokd93",
    "UhTQgJ4jugMqFtEZK8CsIkTqQl22" : "UhTQgJ4jugMqFtEZK8CsIkTqQl22"
  },
  "class_info" : {
    "date_clasname" : "mm",
    "room_number" : "jjhh",
    "subject" : "Science",
    "teacher" : "mm",
    "uid" : "-LiGRe3e1YF_HFrHKMgi"
  }
},
  "Users" : {
"J526G6DB3uONdNM2hzgDYdgokd93" : {
  "Classes" : {
    "-LiGRdN6aoXHfE9wn0TW" : "-LiGRdN6aoXHfE9wn0TW",
    "-LiGRe3e1YF_HFrHKMgi" : "-LiGRe3e1YF_HFrHKMgi"
  },
  "User_info" : {
    "email" : "bob@gmail.com",
    "grade" : "9",
    "name" : "bob"
  }

这是我在UITableView中显示信息的活动的代码:

  class class_custom_cell2: UITableViewCell {
    @IBOutlet weak var cc: UILabel!
    @IBOutlet weak var teacher: UILabel!
    @IBOutlet weak var rn: UILabel!    
}      
class Home: UIViewController , UITableViewDelegate, UITableViewDataSource{
var classes2 = [Class]()
@IBOutlet weak var tb_yourClasses: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
       tb_yourClasses.delegate = self
        tb_yourClasses.dataSource = self

        fetchClass2()
    }

    func fetchClass2() {


        Database.database().reference().child("Users").child(Auth.auth().currentUser!.uid).child("Classes").observe(.childAdded, with: { (snapshot) in

       if let dictionary = snapshot.value as? String{
                let class_id = dictionary
         print(class_id)
       let b = Database.database().reference().child("Classes").child(class_id).child("class_info")
        print(b)
        b.observe(.childAdded, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                let class_object = Class(dictionary: dictionary)
                print(class_object)
                self.classes2.append(class_object)



                DispatchQueue.main.async(execute: {
                    self.tb_yourClasses.reloadData()
                })
            }

            }, withCancel: nil)

            }

        }, withCancel: nil)
    }

    @objc func handleCancel() {
        dismiss(animated: true, completion: nil)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(classes2)
        return classes2.count
       }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

        let data_class2 = classes2[indexPath.row]
    print(data_class2)
        cell.cc?.text = data_class2.date_clasname
        cell.teacher?.text = data_class2.teacher
        cell.rn?.text = data_class2.room_number

        return cell
    }   
 }

谢谢,如果您有任何疑问,请通知我!

0 个答案:

没有答案