【问题标题】:Putting Firebase Data Into Array to Be Put Into Text View (Swift)将 Firebase 数据放入数组以放入文本视图 (Swift)
【发布时间】:2020-05-03 20:49:00
【问题描述】:

所以我一直在尝试将一些 Firebase 数据放入 Swift 中的数组中,这样我就可以将其放入文本字​​段中。一切正常,但是,我的文本视图是空的,不包含行和数据。有谁知道为什么会这样?我的代码在下面(我在顶部导入了 Firebase 和 UIKit,不知道为什么 Stack Overflow 不让我把它放进去……抱歉):

class SummaryEmployeesFeelingViewController: UIViewController, UITableViewDataSource{

    struct Feedback {
        let id: Int
        let feeling: String
        let date: Timestamp
    }


    var feedbackarray = [Feedback]()

    @IBOutlet weak var tableView: UITableView!

    let summarydb = Firestore.firestore()

    override func viewDidLoad() {
        super.viewDidLoad()
        addDatatoArray()
        tableView.dataSource = self
        tableView.tableFooterView = UIView()

    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let feedbackcell = tableView.dequeueReusableCell(withIdentifier: "CellReuseIdentifier")!

        let text = feedbackarray[indexPath.row] as? String

        feedbackcell.textLabel?.text = text

        return feedbackcell
    }

    func addDatatoArray(){
        summarydb.collection("employeefeedback").getDocuments { (snapshot, error) in
            if error == nil && snapshot != nil{
                for document in snapshot!.documents{
                    var idValue = document.get("id")
                    var feelingValue = document.get("feeling")
                    var dateValue = document.get("date")
                    let newEntry = Feedback(id: idValue as! Int, feeling: feelingValue as! String, date: dateValue as! Timestamp)
                    self.feedbackarray.append(newEntry)

                }
        }
    }


}


}

谢谢!

【问题讨论】:

    标签: ios arrays swift xcode firebase


    【解决方案1】:

    获取数据后需要重新加载tableView

     func addDatatoArray(){
        summarydb.collection("employeefeedback").getDocuments { (snapshot, error) in
            if error == nil && snapshot != nil{
                for document in snapshot!.documents{
                    var idValue = document.get("id")
                    var feelingValue = document.get("feeling")
                    var dateValue = document.get("date")
                    let newEntry = Feedback(id: idValue as! Int, feeling: feelingValue as! String, date: dateValue as! Timestamp)
                    self.feedbackarray.append(newEntry)
    
                }
              print("count:\(self.feedbackarray.count)")
              DispatchQueue.main.async {
                tableView.reloadData()
              }
          }
        }
    }
    

    同时设置tableView委托

    override func viewDidLoad() {
            super.viewDidLoad()
            addDatatoArray()
            tableView.dataSource = self
            tableView.delegate = self
            tableView.tableFooterView = UIView()
    
        }
    

    同时更新你的这个方法

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let feedbackcell = tableView.dequeueReusableCell(withIdentifier: "CellReuseIdentifier")!
    
            let feedBack = feedbackarray[indexPath.row] as? Feedback
    
            feedbackcell.textLabel?.text = feedBack.feeling
    
            return feedbackcell
        }
    

    【讨论】:

    • 请检查 print("count:(self.feedbackarray.count)") 并告诉我
    • 我更新了我的答案...在哪里获取反馈数组的计数...我想看看它是否有数据?
    • 您在数组中添加 newEntry ... newEntry 具有 id、日期和感觉,因此它将打印 1 ...所以您应该有一行表格 ...
    • 更新了我的答案...更新您的cellForRowAt 方法...您数组的每个索引都有Feedback 结构...不是字符串...用Feedback 结构投射它跨度>
    • 正如我告诉你的那样......你的反馈数组的每个索引都有结构......所以你用反馈投射它......现在你有反馈对象......它有 id ,感觉和日期...你想在单元格中显示什么?
    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    相关资源
    最近更新 更多