Advertisement
Guest User

Tableview e firebase

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. //
  2. // AziendeVC.swift
  3. // coupon
  4. //
  5. // Created by MacBook Pro on 19/02/18.
  6. // Copyright © 2018 adriano lomonaco. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import FirebaseDatabase
  11.  
  12. class AziendeVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
  13. var Aziende: [Azienda] = []
  14.  
  15. @IBOutlet var tableView: UITableView!
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. tableView.delegate = self
  21. tableView.dataSource = self
  22. }
  23.  
  24. override func viewDidAppear(_ animated: Bool) {
  25. super.viewDidAppear(animated)
  26.  
  27. self.observeAllValue()
  28. self.observeChildAdded()
  29. }
  30.  
  31.  
  32. @IBOutlet weak var Label: UILabel!
  33. func observeAllValue() {
  34. let root = Database.database().reference()
  35.  
  36. root.child("Abbigliamento").observe(.value, with: { (snap) in
  37.  
  38. guard let dictionary = snap.value as? [String : Any] else {
  39. return
  40. }
  41. print("OAV le aziende nel database sono " + "\(dictionary.count)")
  42. print(dictionary.count)
  43.  
  44. var aziendaArray: [Azienda] = []
  45. self.Label.text = "prima del for"
  46. for element in dictionary {
  47. print("sono entrato nel for")
  48. print("element =" + "\(element)")//stampa:(key: "salsiccia", value: buona)
  49. print("element =" + "\(element.value)")//stampa: element = buona
  50. self.Label.text = "dentro il for" //stampa nell'App
  51. guard
  52. let value = element.value as? [String : String],
  53. let name = value["Azienda2"]
  54. else {
  55. return
  56. }
  57. print("value2 =" + "\(value)")//non arriva qui
  58. self.Label.text = "dopo il for"
  59. let azienda = Azienda(Azienda2: name)
  60. aziendaArray.append(azienda)
  61. print("aziendaArray. count = " + "\(aziendaArray.count)")
  62. }
  63.  
  64. self.Aziende = aziendaArray
  65. self.tableView.reloadData()
  66. })
  67. }
  68.  
  69. func observeChildAdded() {
  70. let root = Database.database().reference()
  71. print("\n \n")
  72. root.child("Abbigliamento").observe(.childAdded, with: { (snap) in
  73.  
  74. guard
  75. let dictionary = snap.value as? [String : String],
  76. let name = dictionary["Azienda"]
  77. else { return }
  78.  
  79. let azienda = Azienda(Azienda2: name)
  80. self.Aziende.append(azienda)
  81. let count = self.Aziende.count
  82. print("CA le aziende nel database sono " + "\(count)")
  83. let indexPath = IndexPath.init(row: count-1, section: 0)
  84. self.tableView.beginUpdates()
  85. self.tableView.insertRows(at: [indexPath], with: .left)
  86. self.tableView.endUpdates()
  87. })
  88. }
  89.  
  90. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  91. return Aziende.count
  92. }
  93.  
  94. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  95. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  96. let user = Aziende[indexPath.row]
  97. cell.textLabel?.text = user.Azienda2
  98. return cell
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement