简体   繁体   English

从子文件夹Firebase Swift接收图像数组时出错

[英]Error while receiving image array from the Child's folder Firebase Swift

I am currently trying to receive an array of images with title from my Child's folder to the another offertableview which is connected by button from the detailViewController, but unfortunately I keep getting an error. 我目前正在尝试从我的孩子的文件夹中接收带有标题的图像数组,该图像通过detailViewController中的按钮连接到另一个offertableview,但是不幸的是,我一直收到错误消息。 Below I attached images of my firebase data structure and my mainstoryboard screenshot. 在下面,我附上了Firebase数据结构的图像和我的主板截图。

For the first table view I have a list of the restaurants and upon selecting a cell it transfers to the detail view controller which lists all the details of the restaurant (for that I've created a model of my restaurant) in that detailVC I have a button connected to the offerstableview which lists all the offers of that particular restaurant. 对于第一个表格视图,我有一个餐厅列表,选择一个单元格后,它会转移到详细信息视图控制器,该控制器在该detailVC中列出餐厅的所有详细信息(为此我创建了餐厅的模型)连接到offerstable视图的按钮,其中列出了该特定餐厅的所有要约。

When I click to the button it transfers to the offers table view which results to the application shut down due to the error. 当我单击该按钮时,它会转移到offers表视图,由于错误导致应用程序关闭。

my offers tableview code: 我的优惠表格视图代码:

var ref: DatabaseReference!
var offerImageArray = [String]()
var titleArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    fetchBars()
}

func fetchBars(){

    ref.child("Paris").observeSingleEvent(of: .value, with: { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let imageSnap = snap.childSnapshot(forPath: "offers")
            let dict = imageSnap.value as! [String: Any]
            let imageUrl = dict["offer_image"] as? String
            let titleUrl = dict["offer_title"] as? String
            self.offerImageArray = [imageUrl! as String]
            self.titleArray = [titleUrl! as String]
        }
    })
        self.tableView.reloadData()
}

override func numberOfSections(in tableView: UITableView) -> Int { return 1 } 覆盖func numberOfSections(在tableView中:UITableView)-> Int {return 1}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCell", for: indexPath) as! OffersTableViewCell

    cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
    cell.titleLabel.text = titleArray[indexPath.row]
           return cell
}

Xcode error: Xcode错误:

2017-08-16 10:26:33.652 Applic[1174] [Firebase/Analytics][I-ACS003007] Successfully created Firebase Analytics App Delegate Proxy automatically. 2017-08-16 10:26:33.652 Applic [1174] [Firebase / Analytics] [I-ACS003007]成功自动自动创建了Firebase Analytics App委托代理。 To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 2017-08-16 10:26:33.826 Applic[1174] [Firebase/Analytics][I-ACS032003] iAd framework is not linked. 要禁用代理,请将Info.plist 2017-08-16 10:26:33.826 Applic [1174] [Firebase / Analytics] [I-ACS032003]中的标志FirebaseAppDelegateProxyEnabled设置为NO。iAd框架未链接。 Search Ad Attribution Reporter is disabled. 搜索广告归因报告器已禁用。 2017-08-16 10:26:33.828 Applic[1174] [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-16 10:26:33.828应用[1174] [Firebase / Analytics] [I-ACS023012] Firebase Analytics启用致命错误:在展开可选值时意外发现nil

NewFirbaseStructure

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import SDWebImage


class OffersTableVC: UITableViewController {
    var ref: DatabaseReference!
   var offerImageArray = [String]()
    var titleArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchBars()
    }

    func fetchBars(){

        Database.database().reference().child("paris").observeSingleEvent(of: .value, with: { (snapshot) in

            print("Main Snapshot is \(snapshot)")

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let imageSnap = snap.childSnapshot(forPath: "offers")



                if let snapDict = imageSnap.value as? [String:AnyObject] {
                    let dictKeys = [String](snapDict.keys)
                    print(dictKeys)
                    let dictValues = [AnyObject](snapDict.values)
                    print(dictValues)


                    for each in dictValues{
                        let imageUrl = each["offer_image"] as? String
                        print(imageUrl!)
                        self.offerImageArray.append(imageUrl!)

                    }
                    self.tableView.dataSource = self
                    self.tableView.delegate = self
                    self.tableView.reloadData()

                }





//                let dict = imageSnap.value as! [String: Any]
//                let imageUrl = dict["offer_image"] as? String
//                let titleUrl = dict["offer_title"] as? String
//                self.offerImageArray = [imageUrl! as String]
//                self.titleArray = [titleUrl! as String]
            }
        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    // MARK: - Table view data source

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return offerImageArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCell", for: indexPath) as! OffersTableViewCell

        cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
//        cell.titleLabel.text = titleArray[indexPath.row]
               return cell
    }

}

1) in Appdelegate.swift add selectedBarname as follow 1)在Appdelegate.swift中添加selectedBarname如下

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var selectedBarName = String()

2) MainTableVc Add following code 2)MainTableVc添加以下代码

After declaration of Class 上课申报后

let appDelegate = UIApplication.shared.delegate as! AppDelegate

in prepareForSegue 在prepareForSegue中

if segue.identifier == "DetailView", let bar = selectedBar{
            appDelegate.selectedBarName = bar.barName

3) OfferTableVc 3)OfferTableVc

now just call this function and Done but do not call your fetchBars now just getOffers 现在只需调用此函数并完成,但不调用您的fetchBars现在只需getOffers

func getOffers() {
    let databaseRef = Database.database().reference().child("aktau")
        databaseRef.queryOrdered(byChild: "bar_name").queryEqual(toValue: self.appDelegate.selectedBarName).observe(.value, with: { snapshot in

        if ( snapshot.value is NSNull ) {
            print("not found)")

        } else {
            print(snapshot.value!)
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let imageSnap = snap.childSnapshot(forPath: "offers")
                if let snapDict = imageSnap.value as? [String:AnyObject] {

                    let dictValues = [AnyObject](snapDict.values)


                    for each in dictValues{
                        let imageUrl = each["offer_image"] as? String
                        print(imageUrl!)
                        self.offerImageArray.append(imageUrl!)

                    }
                    self.tableView.dataSource = self
                    self.tableView.delegate = self
                    self.tableView.reloadData()

                }

            }


        }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM