致命错误:从Firebase检索数据以添加到Swift数组时,索引超出范围

时间:2016-07-12 21:44:52

标签: ios swift firebase firebase-realtime-database

我遇到错误"索引超出范围"从Firebase 3.0检索数据并尝试将其添加到Swift数组时。我的意图是,当数组从Firebase实时数据库异步检索数据时为空时,视图应呈现显示每个标签为" Not Selected"。关于如何做到这一点的任何意见将不胜感激。

Firebase层次结构:

  • service_history
    • UID
      • 自动ID
        • label1Selected
        • label2Selected
        • label3Selected
        • label4Selected
        • label5Selected
        • label6Selected
        • label7Selected

以下是View Controller代码:

import UIKit
import Firebase

class ServiceHistoryTableViewController: UITableViewController {

    //Vars
    var orderHistoryArray = [ServiceOrder]()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Hide navigation bar
        self.navigationController?.navigationBarHidden = true

        startSynchronizingServiceHistory()
    }

    func startSynchronizingServiceHistory() {

        var serviceOrder = ServiceOrder()

        //Retrieve Service History for a given UID
        FIRDatabase.database().reference().child("service_history").child(FIRAuth.auth()!.currentUser!.uid).observeEventType(.Value, withBlock: { snapshot in

            self.orderHistoryArray = []

            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                for snap in snapshots {
                    if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key
                        serviceOrder = ServiceOrder(key: key, dictionary: postDictionary)
                        //Populate array
                        self.orderHistoryArray.append(serviceOrder)
                    }
                }
                self.tableView.reloadData()
            }
        }){ (error) in
            print(error.localizedDescription)
        }
    }

    override func viewWillAppear(animated: Bool) {
        self.tableView.reloadData()
    }

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

    //--------------------------------------------------------
    // MARK: Hide status bar
    //--------------------------------------------------------
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            //return orderHistoryArray.count
        return 1
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("customSummaryCell", forIndexPath: indexPath) as! SummaryCustomTableViewCell

        cell.serviceOrderSummaryLabel.text = "SERVICE ORDER SUMMARY"
        cell.dateLabel.text = "Created:"

        if (orderHistoryArray[1].createdTimestamp.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.date.text = orderHistoryArray[0].createdTimestamp
        }
        if (orderHistoryArray[1].plumbingLeakEmergencyChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.date.text = "Label 1 selected"
        }
        if (orderHistoryArray[2].overflowingDrainEmergencyChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 2 selected"
        }
        if (orderHistoryArray[3].drainStoppageEmergencyChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 3 selected"
        }
        if (orderHistoryArray[4].fixturesInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 4 selected"
        }
        if (orderHistoryArray[5].waterHeaterInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 5 selected"
        }
        if (orderHistoryArray[6].dishwasherInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 6 selected"
        }
        if (orderHistoryArray[7].garbageDisposalInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 7 selected"
        }
        if (orderHistoryArray[8].iceMakerSupplyLinesInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 8 selected"
        }
        if (orderHistoryArray[9].additionalHoseBibsInstallationChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 9 selected"
        }
        if (orderHistoryArray[10].fixturesRepairChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 10 selected"
        }
        if (orderHistoryArray[11].waterHeaterRepairChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 11 selected"
        }
        if (orderHistoryArray[12].drainCleaningServiceChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 12 selected"
        }
        if (orderHistoryArray[13].waterHeaterCleanOutServiceChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 13 selected"
        }
        if (orderHistoryArray[14].leakDetectionServiceChecked.isEmpty) {
            cell.date.text = "Not selected"
        } else {
            cell.label1.text = "Label 14 selected"
        }
        cell.more.text = "More..."

        return cell

    }

}

以下是我要渲染的表格视图: enter image description here

0 个答案:

没有答案