2017-08-14 50 views
-1

我目前正在建立一個列出各種餐館的項目。在我的項目中,我使用主表視圖來顯示餐館列表,並在點擊單元格時將其傳遞給視圖控制器,該視圖控制器顯示有關餐廳的基本信息。在詳細的視圖控制器中有一個名爲「Offers」的按鈕,點擊後會打開另一個tableview,顯示該餐廳的一系列優惠信息。 (請看附圖)檢索孩子的文件夾數據Firebase Swift

現在我只能檢索我的餐廳列表,詳細顯示詳細的視圖控制器,但我不知道如何顯示每個餐廳的優惠清單。 請幫助我,並提前謝謝你。

代碼: 餐廳/酒吧型號:

import Foundation 
import Firebase 
import FirebaseDatabase 

struct Bar{ 
    let id: String 
    let barName: String 
    let barAddress: String 
    let barMainImage: String 
    let barInfo: String 
    let barFullAddress: String 
    let averagePrice: String 

    let disabledIcon: String 
    let parkingIcon: String 
    let wifiIcon: String 
    let offerImage: String 

    static func bar(from snapshot: DataSnapshot) -> Bar?{ 
     let barDict = snapshot.value as? [String : AnyObject] ?? [:] 
     guard let barName = barDict["bar_name"] as? String, 
     let barAddress = barDict["bar_short_address"] as? String, 
     let barMainImage = barDict["bar_main_image"] as? String, 
     let barInfo = barDict["bar_info"] as? String, 
     let barFullAddress = barDict["bar_full_address"] as? String, 
     let averagePrice = barDict["average_price"] as? String, 

     let disabledIcon = barDict["disabled_icon"] as? String, 
     let parkingIcon = barDict["parking_icon"] as? String, 
     let wifiIcon = barDict["wifi_icon"] as? String, 
let offers = barDict["offers"] as? [String:Any], 
    let offerImage = offers["offer_image"] as? String, 
     let id = String(snapshot.key) 
     else {return nil} 
    let bar = Bar (id: id, barName: barName, barAddress: barAddress, barMainImage: barMainImage, barInfo: barInfo, barFullAddress: barFullAddress, averagePrice: averagePrice, disabledIcon: disabledIcon, parkingIcon: parkingIcon, wifiIcon : wifiIcon, offerImage: offerImage) 

    return bar 
} 

}

首先表視圖控制器 - 列出所有的餐館:

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

class MainTableVC: UITableViewController { 


@IBOutlet weak var MenuBar: UIBarButtonItem! 

@IBOutlet weak var cityButton: UIButton! 

var ref: DatabaseReference! 
var bars = [Bar]() 
var selectedBar: Bar? 
var refreshTableView = UIRefreshControl() 
var selectedCity:String! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 

    //back button title removal 
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil) 


    // SlideOutMenu 
    MenuBar.target = self.revealViewController() 
    MenuBar.action = #selector(SWRevealViewController.revealToggle(_:)) 
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 

    ref = Database.database().reference() 

    fetchBars() 
    // for pull to refresh 
    refreshTableView.tintColor = UIColor.white 
    self.tableView.addSubview(refreshTableView) 
    refreshTableView.addTarget(self, action: #selector(MainTableVC.refreshData), for: UIControlEvents.valueChanged) 


} 


// for pull to refress 
func refreshData() { 
    //removes data before updating and entering new one 
    self.bars.removeAll() 
    ref.child("Paris").observe(.childAdded, with: { (snapshot) in 
     guard let bar = Bar.bar(from: snapshot) else {return} 
     self.bars.append(bar) 
     self.refreshTableView.endRefreshing() 
     self.tableView.reloadData() 
    }) 
} 

func fetchBars(){ 

    ref.child("Paris").observe(.childAdded, with: { (snapshot) in 
     guard let bar = Bar.bar(from: snapshot) else {return} 
     self.bars.append(bar) 
     self.tableView.reloadData() 
    }) 
    self.refreshTableView.endRefreshing() 
} 

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

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return bars.count 
} 


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

    let bar = bars[indexPath.row] 
    cell.RestName.text = bar.barName 
    cell.RestAddress.text = bar.barAddress 
    cell.RestImage.sd_setImage(with: URL(string: bar.barMainImage)) 


    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    selectedBar = bars[indexPath.row] 
    performSegue(withIdentifier: "DetailView", sender: self) 
} 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "DetailView", let bar = selectedBar{ 
     let detailVC = segue.destination as! DetailViewController 
     detailVC.selectedBar = bar 
    } 
} 

詳細視圖控制器 - 列出所有細節inf爲每個餐廳準備:

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

class DetailViewController: UIViewController { 

@IBOutlet weak var MyScroll: UIScrollView! 




@IBOutlet weak var RestDetailView: UIView! 
@IBOutlet weak var restName: UILabel! 

@IBOutlet weak var restTypeOfFood: UILabel! 

@IBOutlet weak var restClockImage: UIImageView! 

@IBOutlet weak var restOpenHours: UILabel! 

@IBOutlet weak var restInfoView: UIView! 

@IBOutlet weak var averagePrice: UILabel! 

@IBOutlet weak var InfoAboutRest: UILabel! 

@IBOutlet weak var FullAddressLabel: UILabel! 

@IBOutlet weak var disabledIcon: UIImageView! 

@IBOutlet weak var parkingIcon: UIImageView! 

@IBOutlet weak var wifiIcon: UIImageView! 

@IBOutlet weak var OpeningHoursLabel: UILabel! 





@IBOutlet weak var ImageViewD: UIImageView! 

var selectedBar:Bar?

override func viewDidLoad() { 
    super.viewDidLoad() 


    self.RestDetailView.layer.borderColor = UIColor.white.cgColor 
    self.RestDetailView.layer.borderWidth = 1 
    self.restInfoView.layer.borderColor = UIColor.white.cgColor 
    self.restInfoView.layer.borderWidth = 1 

    if let bar = selectedBar{ 
     restName.text = bar.barName 
     ImageViewD.sd_setImage(with: URL(string: bar.barMainImage)) 
     InfoAboutRest.text = bar.barInfo 
     averagePrice.text = bar.averagePrice 
     FullAddressLabel.text = bar.barFullAddress 
     disabledIcon.sd_setImage(with: URL(string: bar.disabledIcon)) 
     parkingIcon.sd_setImage(with: URL(string: bar.parkingIcon)) 
     wifiIcon.sd_setImage(with: URL(string: bar.wifiIcon)) 

    } 

} 






/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 

}

IMAGES

[火力地堡樹] [1] [MainStoryBoard] [2]

OffersTableView: 


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


class OffersTableVC: UITableViewController { 
var ref: DatabaseReference! 
var bars = [Bar]() 
var selectedBar: Bar? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    fetchBars() 
} 
func fetchBars(){ 

    ref.child("aktau").observe(.childAdded, with: { (snapshot) in 
     guard let bar = Bar.bar(from: snapshot) else {return} 
     self.bars.append(bar) 
     self.tableView.reloadData() 
    }) 

} 


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

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 1 
} 


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

    let bar = bars[indexPath.row] 
    cell.offerImageView.sd_setImage(with: URL(string: bar.offerImage)) 
    return cell 
} 

錯誤在我的模型:

ModelError

+0

這個問題中代碼完全太多了。答案可能很簡單,但沒有看到您的Firebase結構,這只是猜測。請將您的Firebase結構中的代碼片段作爲文本添加進去,不要包含圖片(Firebase控制檯 - >導出JSON),並將代碼簡化爲一個簡單示例。請花點時間回顧一下[我如何提出一個好問題?](https://stackoverflow.com/help/how-to-ask)和[如何創建一個最小,完整和可驗證的示例](https:/ /stackoverflow.com/help/mcve)。清理它,我們會看看! – Jay

回答

0

必須解析 「報價」 像

let offers = barDict["offers"] as? [String:Any] 

然後你必須分析這個字典一樣

var offersArray = [Offer]() 

    for item in offers { 

    guard let offerimage = item["offerImage"] as? String, ... { 
     var offer = Offer(offerimage) 
     offersArray.append(offer) 
    } 
} 


class Offer { 

let offerImageUrl:String 
// Other Properties 

    init(string: offerImageUrl){ 
    self.offerImageUrl = offerImageurl 
    //Other Properties 
    } 
} 

我希望你明白我的想法。現在你有一個數組與您的優惠:)

+0

我應該爲這些優惠創建一個Struct嗎?或者我應該在「let wifiIcon = barDict [」wifi_icon「] as?String,」之後直寫「? – didayo

+0

直接後,Wifiicon很好 – Peiper

+0

我已更新我的代碼與編輯),但不幸的是,它現在不顯示任何東西)甚至我的餐廳列表)我不知道爲什麼) – didayo