ios - 为什么我的 UITableViewCells 不显示信息?

标签 ios swift uitableview firebase firebase-realtime-database

当我启动我的应用程序时,它应该会在我的数据库中显示所有参与方的姓名和位置数据,但事实并非如此。我很确定我得到了正确的数据,但我不知道出了什么问题。如何让我的表格显示所有数据并在每次添加新数据时更新?

import UIKit
import Firebase
import FirebaseDatabase

class FindPartiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var parties = [party]()

    @IBOutlet weak var partyTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        partyTable.delegate = self
        partyTable.dataSource = self

        let ref = FIRDatabase.database().reference()

        ref.child("parties").queryOrderedByKey().observe(.childAdded, with: {
            snapshot in
            let snapshotValue = snapshot.value as? NSDictionary
            let name = snapshotValue?["name"] as! String
            let location = snapshotValue?["location"] as! String
            let description = snapshotValue?["description"] as! String

            self.parties.append(party(name: name, description: description, location: location))
        })

    }


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

    // creates the number of cells in the table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return parties.count
    }

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

        // Make table cells the show the user name
        var cell = partyTable.dequeueReusableCell(withIdentifier: "Cell")

        let nameLabel = cell?.viewWithTag(1) as! UILabel
        nameLabel.text = parties[indexPath.row].name

        let locationLabel = cell?.viewWithTag(2) as! UILabel
        locationLabel.text = parties[indexPath.row].location


        return cell!
    }

}

这是我的数据库设置:

testApp
      parties
           -KUUzxkssjWoSZ22QS8r
                description: ""
                location: "Here"
                name: "party 1"
          -KUV0ObFVbPyI_SJ0f3F
                description: ""      
                location: "There"
                name: "party 2"

最佳答案

更新数据模型后需要重新加载 TableView :

ref.child("parties").queryOrderedByKey().observe(.childAdded, with: {
    snapshot in
    let snapshotValue = snapshot.value as? NSDictionary
    let name = snapshotValue?["name"] as! String
    let location = snapshotValue?["location"] as! String
    let description = snapshotValue?["description"] as! String

    self.parties.append(party(name: name, description: description, location: location))

    DispatchQueue.main.async {
        partyTable.reloadData()
    }
})

关于ios - 为什么我的 UITableViewCells 不显示信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40144773/

相关文章:

iphone - uitableviewcell 交替背景单元格颜色,即使对于没有数据的单元格也是如此

ios - 无法访问 NSImage 对象或无法访问 "import NSImage.h"

ios - 仅在从urls数组加载所有图像之后才激事件画

ios - 线程组内存中是否有atomic_fetch_add_explicit原子?

Swift 3、RxAlamofire 和映射到自定义对象

ios - 是否可以将 subview 发送到 Storyboard中创建的元素的后面?

swift 3 : How do I tap a button inside cell and pass in that indexPath of that cell?

ios - 通过广告跟踪 Facebook 应用安装

ios - 在 xcode 项目中使用未解析的标识符 'cell'

ios - 使用 Segue 的主细节 View