Swift:总是从 Firebase 获取最新的项目

标签 swift firebase dictionary firebase-realtime-database

我的目标是构建一个简单的应用程序:

  • 通过从 Firebase 获取项目而拥有 UITableViewCell
  • 当点击时,每个单元格都会执行到另一个 ViewController 的 segue。
  • 在呈现的 ViewController 中显示所获取项目的更多详细信息。

到目前为止我所做的工作:

  • 我可以成功地从数据库中获取数据并将其放入字典中。我还可以根据这些数据填充 UITableViewCell
  • Cells 在点击时根据需要呈现一个新的 ViewController

问题是,无论我点击哪个单元格,我的 ViewController 始终显示 Firebase 数据库中最近最少添加的项目。让我提供我的数据库结构和我的 Swift 代码:

Firebase 结构:

simple-app:
└── upcoming:
    └── fourth:
        └── desc: "description for the fourth item"
        └── name: "fourth item"
    └── third:
        └── desc: "description for the third item"
        └── name: "third item"
    └── second:
        └── desc: "description for the second item"
        └── name: "second item"
    └── first:
        └── desc: "description for the first item"
        └── name: "first item"

Swift代码:

import Foundation
import UIKit
import Firebase

class Upcoming: UITableViewController{
    @IBOutlet weak var upcomingItems: UITableView!
    var itemName: String?
    var itemDesc: String?

    var ref: DatabaseReference!
    var refHandle: UInt!

    var itemList = [Item]()
    let cellId = "cellId"

    override func viewDidLoad() {
        ref = Database.database().reference()
        fetchItems()
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
        itemName = itemList[indexPath.row].name
        itemDesc = itemList[indexPath.row].desc

        cell.textLabel?.text = itemName
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "DetailedInfo", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "DetailedInfo" {
            var vc = segue.destination as! DetailedInfo
            vc.name = itemName
            vc.desc = itemDesc
        }
    }

    func fetchItems(){
        refHandle = ref.child("upcoming").observe(.childAdded, with: {(snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject]{
                print(dictionary) // dictionary is as desired, no corruption
                let item = Item()
                item.setValuesForKeys(dictionary)
                self.itemList.append(token)

                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        })
    }
}

我可以根据需要看到具有四个不同名称的四个不同单元格,但无论点击哪个单元格,下一个 ViewController 都会显示 namedesc 最近最少添加的项目的值,即 first,来自数据库。感谢任何想法来解决这个问题。

最佳答案

itemName 和 itemDesc 将永远是最后一个单元格,因为你将它们写在 cellForRow 中, 在 didSelectRow 中设置项目

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
        cell.textLabel?.text = itemList[indexPath.row].name
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        itemName = itemList[indexPath.row].name
        itemDesc = itemList[indexPath.row].desc
        performSegue(withIdentifier: "DetailedInfo", sender: self)
    }

关于Swift:总是从 Firebase 获取最新的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276290/

相关文章:

ios - 我们应该将所有数据存储在最新版本的 firebase 上吗?

javascript - Cesiumjs 缩放到鼠标

Swift 4 获取定时定时器的当前时间间隔

c - 如何访问映射到 swift 结构的内存缓冲区?

android - 如何删除 Android Studio 中的重复文件

java - 是什么原因导致打字失败?

ios - 带有自动布局的 Storyboard UITableViewCell

ios - 如何为 MIDI 事件准确设置时间戳?

python - 比较字典并更新结果列表

python - 如果字典中的值相等,则返回键 python