Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AziendeVC.swift
- // coupon
- //
- // Created by MacBook Pro on 19/02/18.
- // Copyright © 2018 adriano lomonaco. All rights reserved.
- //
- import UIKit
- import FirebaseDatabase
- class AziendeVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
- var Aziende: [Azienda] = []
- @IBOutlet var tableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.delegate = self
- tableView.dataSource = self
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- self.observeAllValue()
- self.observeChildAdded()
- }
- @IBOutlet weak var Label: UILabel!
- func observeAllValue() {
- let root = Database.database().reference()
- root.child("Abbigliamento").observe(.value, with: { (snap) in
- guard let dictionary = snap.value as? [String : Any] else {
- return
- }
- print("OAV le aziende nel database sono " + "\(dictionary.count)")
- print(dictionary.count)
- var aziendaArray: [Azienda] = []
- self.Label.text = "prima del for"
- for element in dictionary {
- print("sono entrato nel for")
- print("element =" + "\(element)")//stampa:(key: "salsiccia", value: buona)
- print("element =" + "\(element.value)")//stampa: element = buona
- self.Label.text = "dentro il for" //stampa nell'App
- guard
- let value = element.value as? [String : String],
- let name = value["Azienda2"]
- else {
- return
- }
- print("value2 =" + "\(value)")//non arriva qui
- self.Label.text = "dopo il for"
- let azienda = Azienda(Azienda2: name)
- aziendaArray.append(azienda)
- print("aziendaArray. count = " + "\(aziendaArray.count)")
- }
- self.Aziende = aziendaArray
- self.tableView.reloadData()
- })
- }
- func observeChildAdded() {
- let root = Database.database().reference()
- print("\n \n")
- root.child("Abbigliamento").observe(.childAdded, with: { (snap) in
- guard
- let dictionary = snap.value as? [String : String],
- let name = dictionary["Azienda"]
- else { return }
- let azienda = Azienda(Azienda2: name)
- self.Aziende.append(azienda)
- let count = self.Aziende.count
- print("CA le aziende nel database sono " + "\(count)")
- let indexPath = IndexPath.init(row: count-1, section: 0)
- self.tableView.beginUpdates()
- self.tableView.insertRows(at: [indexPath], with: .left)
- self.tableView.endUpdates()
- })
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return Aziende.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- let user = Aziende[indexPath.row]
- cell.textLabel?.text = user.Azienda2
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement