Firebase / Swift - 从Firebase输出数据出错

时间:2017-11-13 07:57:00

标签: ios swift uitableview firebase firebase-realtime-database

我遇到了从Firbase正确输出数据的问题。我花了很多时间并尝试了很多不同的方法,创建不同的数据结构等,但我总是得到崩溃或零输出。我当前的代码正在工作并将数据从firebase输出到表视图中,但是数组未对齐,并且特定用户的一个数组中的字段与另一个不相关,这是不正确的。

我想有一个更好的方法可以做到这一点,所以任何帮助或正确方向的一点都将非常感激。也请放轻松我,我知道这个:)

这是我的firebase结构:

这是我的租赁模式:

struct RentalObjects {

    var title: [String : AnyObject] = [:]
    var rentalType: [String : AnyObject] = [:]
    var dateAval: [String : AnyObject] = [:]
    var location: [String : AnyObject] = [:]
    var price: [String : AnyObject] = [:]
    var bond: [String : AnyObject] = [:]
    var pets: [String : AnyObject] = [:]
    var descripton: [String : AnyObject] = [:]

}

这是我输出到表格视图的VC的代码:

import UIKit
import FirebaseDatabase


class RentalTableViewVC: UIViewController, UITableViewDataSource, UITableViewDelegate{

    @IBOutlet weak var rentalImage: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var rentalTitle: UILabel!
    @IBOutlet weak var rentalPrice: UILabel!


    var rentalsObject = RentalObjects()
    var databaseRef:DatabaseReference?
    var handle: DatabaseHandle?

    var arrayOfTitles = [String?]()
    var arrayOfBond = [String?]()



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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

        for (_, value) in rentalsObject.title {
            arrayOfTitles.append(value as? String)

        }

        for (_, value) in rentalsObject.bond {

            arrayOfBond.append(value as? String)

        }


        cell.textLabel?.text = ("Title: \(String(describing: arrayOfTitles[indexPath.row]!)), Bond: \(String(describing: arrayOfBond[indexPath.row]!))")



        return cell
    }


    @IBAction func backPressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
    super.viewDidLoad()


        databaseRef = Database.database().reference().child("Rentals")
        databaseRef?.observe(DataEventType.value, with: { (snapshot) in

            for rentals in snapshot.children.allObjects as! [DataSnapshot] {





        switch rentals.key {

                case "title" :
                    let titleObj = rentals.value as! [String : AnyObject]

                    self.rentalsObject.title = titleObj

                case "rentalType":
                    let rentalTypeObj = rentals.value as! [String : AnyObject]
                    self.rentalsObject.rentalType = rentalTypeObj

                case "dateAval":
                    let dateAvalObj = rentals.value as! [String : AnyObject]
                    self.rentalsObject.dateAval = dateAvalObj
                case "location":
                    let locationObj = rentals.value as! [String : AnyObject]
                    self.rentalsObject.location = locationObj

                case "price" :
                    let priceObj = rentals.value as! [String : AnyObject]
                    self.rentalsObject.price = priceObj

                case "bond" :
                    let bondObj = rentals.value as! [String: AnyObject]
                    self.rentalsObject.bond = bondObj

                case "pets" :
                    let petsObj = rentals.value as! [String: AnyObject]
                    self.rentalsObject.pets = petsObj

                case "description":
                    let desObj = rentals.value as! [String: AnyObject]
                    self.rentalsObject.descripton = desObj

                default:
                    break

                }

            }
            self.tableView.reloadData()
        })

    }

}

1 个答案:

答案 0 :(得分:0)

使用.childAdded而不是.value。 ChildAdded获取每个对象,而Value获取所有对象。你应该从' cellForRowAr'中删除for循环。方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

       cell.textLabel?.text = ("Title: \(String(describing: arrayOfTitles[indexPath.row]!)), Bond: \(String(describing: arrayOfBond[indexPath.row]!))")

       return cell
}



override func viewDidLoad() {
   super.viewDidLoad()

   databaseRef = Database.database().reference().child("Rentals")
   databaseRef?.observe(.childAdded, with: { (snapshot) in

       if let dictionary = snapshot.value as? [String: AnyObject] {

          switch snapshot.key {

             case "title" :
             _ = dictionary.map{self.arrayOfTitles.append($0.value as? String)}
             case "rentalType":

             case "dateAval":

             case "location":

             case "price" :

             case "bond" :
             _ = dictionary.map{self.arrayOfBond.append($0.value as? String)}
             case "pets" :

             case "description":

             default:
                break
           }
        }
     })
     self.tableView.reloadData()
}