繁体   English   中英

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

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

我目前正在尝试从我的孩子的文件夹中接收带有标题的图像数组,该图像通过detailViewController中的按钮连接到另一个offertableview,但是不幸的是,我一直收到错误消息。 在下面,我附上了Firebase数据结构的图像和我的主板截图。

对于第一个表格视图,我有一个餐厅列表,选择一个单元格后,它会转移到详细信息视图控制器,该控制器在该detailVC中列出餐厅的所有详细信息(为此我创建了餐厅的模型)连接到offerstable视图的按钮,其中列出了该特定餐厅的所有要约。

当我单击该按钮时,它会转移到offers表视图,由于错误导致应用程序关闭。

我的优惠表格视图代码:

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()
}

覆盖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错误:

2017-08-16 10:26:33.652 Applic [1174] [Firebase / Analytics] [I-ACS003007]成功自动自动创建了Firebase Analytics App委托代理。 要禁用代理,请将Info.plist 2017-08-16 10:26:33.826 Applic [1174] [Firebase / Analytics] [I-ACS032003]中的标志FirebaseAppDelegateProxyEnabled设置为NO。iAd框架未链接。 搜索广告归因报告器已禁用。 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)在Appdelegate.swift中添加selectedBarname如下

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var selectedBarName = String()

2)MainTableVc添加以下代码

上课申报后

let appDelegate = UIApplication.shared.delegate as! AppDelegate

在prepareForSegue中

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

3)OfferTableVc

现在只需调用此函数并完成,但不调用您的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