Skip to content

Instantly share code, notes, and snippets.

@lizolni
Last active October 6, 2016 23:34
Show Gist options
  • Save lizolni/6f497e4f237a9883ceccec0e90c46802 to your computer and use it in GitHub Desktop.
Save lizolni/6f497e4f237a9883ceccec0e90c46802 to your computer and use it in GitHub Desktop.
firebase problem2
//
// StoreTableViewController.swift
// TaipeiGoodCoffee
//
// Created by Allen on 2016/10/2.
// Copyright © 2016年 Allen. All rights reserved.
//
import UIKit
import Firebase
class StoreTableViewController: UITableViewController {
let flavorSet = NSUserDefaults.standardUserDefaults()
let conditionRef = FIRDatabase.database().reference()
var stores = [Stores]()
override func viewDidLoad() {
super.viewDidLoad()
getFlavorSelectData1()
// let getFirebase = FirebaseData()
// getFirebase.getFlavorSelectData()
// getFirebase.getFlavorSelectData(completion: {})
// getFirebase.delegate = self
// getFirebase.stores
//getFirebase.getFlavorSelectData2()
//getFirebase.getFlavorSelectData3()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(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 stores.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("storeCell", forIndexPath: indexPath) as! StoreTableViewCell
// print("address:\(stores[indexPath.row].storeAddress)")
// print("name:\(stores[indexPath.row].storeName)")
// print("phone:\(stores[indexPath.row].storePhone)")
// print("image:\(stores[indexPath.row].storeImage)")
// print("food:\(stores[indexPath.row].isFood)")
// print("pet:\(stores[indexPath.row].isPet)")
// print("wifi:\(stores[indexPath.row].isWifi)")
// print("drink:\(stores[indexPath.row].isDrink)")
// print("fbpage:\(stores[indexPath.row].fbPage)")
// print("imageid:\(stores[indexPath.row].storeImageID)")
// print("storeid:\(stores[indexPath.row].storeID)")
// print("servicetime \(stores[indexPath.row].serviceTime)")
cell.storeName.text = stores[indexPath.row].storeName
cell.storeAdd.text = stores[indexPath.row].storeAddress
cell.storePhone.text = stores[indexPath.row].storePhone
cell.storeImage.image = UIImage(named:stores[indexPath.row].storeImage)
cell.serviceTime.text = stores[indexPath.row].serviceTime
if stores[indexPath.row].isFood == "Y"{
cell.isFood.image = UIImage(named: stores[indexPath.row].isFood)
} else if stores[indexPath.row].isDrink == "Y" {
cell.isDrink.image = UIImage(named: stores[indexPath.row].isDrink)
} else if stores[indexPath.row].isPet == "Y" {
cell.isPet.image = UIImage(named: stores[indexPath.row].isPet)
} else if stores[indexPath.row].isWifi == "Y" {
cell.isWIFI.image = UIImage(named: stores[indexPath.row].isWifi)
}
//重新整理tableview
self.tableView.reloadData()
return cell
}
func getFlavorSelectData1() {
//抓取NSUserDefault 1-3 的值
// var n = 1
// for select in 1...3 {
// if let select = self.flavorSet.objectForKey("FlavorSelect_\(n)") as? NSNumber{
// n += 1
// }
let select = self.flavorSet.objectForKey("FlavorSelect_1")
print(select)
conditionRef.child("coffee/Products").queryOrderedByChild("flavorID").queryEqualToValue("\(select)").observeEventType(.Value, withBlock:{ snapshot in
print ("procuts KEY: \(snapshot.key) . products value: \(snapshot.value)")
for child in snapshot.children {
let storeID_1 = child.value["storeID"]
guard let id_1 = storeID_1 as? String else{
fatalError()
}
//依照productID取得Store資料
self.conditionRef.child("coffee/Stores").queryOrderedByChild("storeID").queryEqualToValue(id_1).observeEventType(.ChildAdded, withBlock:{ snapshot in
print ("stores KEY\(snapshot.key) . Store Value\(snapshot.value)")
if let messengerAdd = snapshot.value as? NSDictionary {
let getStore = Stores(data: messengerAdd)
// //判斷重複store的開關
// var haveSameStore = false
//
// //判斷重複
// for store in self.stores {
// if store.storeID == getStore.storeID {
// haveSameStore = true
// }
// }
// //重複為false的時候才把store加到stores array
// if !haveSameStore {
// self.stores.append(getStore)
// }else{
// print("add to array fails")
// }
self.stores.append(getStore)
for a in self.stores{
print("AAAAAA \(a.storeName)")
}
//重新整理tableview
self.tableView.reloadData()
}else {
print("Please check your Network")
}
})
}
})
}
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment