Guest User

Untitled

a guest
Nov 18th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. "Москва, Электрозаводская, 21"
  2. "Москва, Арбат, Староконюшенный переулок, дом 43"
  3. "Москва, улица 1-я Бухвостова, дом 12/11, корпус 53"
  4.  
  5. class PhotoStudiosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  6.  
  7. @IBOutlet weak var tableView: UITableView!
  8.  
  9. var theStudios: [Studio] = []
  10.  
  11. var studiosRef: DatabaseReference!
  12.  
  13. override func viewWillAppear(_ animated: Bool) {
  14. super.viewWillAppear(true)
  15.  
  16. tableView.estimatedRowHeight = 475
  17. tableView.rowHeight = UITableViewAutomaticDimension
  18.  
  19. loadDataFromFirebase()
  20.  
  21. }
  22.  
  23. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  24.  
  25. return theStudios.count
  26. }
  27.  
  28. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  29. let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! PhotoStudiosTableViewCell
  30.  
  31. cell.addressLabel.text = theStudios[indexPath.row].studioAddress
  32.  
  33. return cell
  34. }
  35.  
  36. func loadDataFromFirebase() {
  37.  
  38. studiosRef = Database.database().reference(withPath: "Addresses")
  39.  
  40. let time = DispatchTime.now() + 0.5
  41.  
  42. studiosRef.observe(.value, with: { (snapshot) in
  43.  
  44. DispatchQueue.main.asyncAfter(deadline: time) {
  45.  
  46. for imageSnap in snapshot.children {
  47.  
  48. let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)
  49.  
  50. self.theStudios.append(studioObj)
  51.  
  52. }
  53.  
  54. self.tableView.reloadData()
  55.  
  56. }
  57.  
  58. })
  59.  
  60. }
  61.  
  62.  
  63.  
  64. class Studio {
  65.  
  66. var ref: DatabaseReference!
  67. var studioAddress: String = ""
  68.  
  69. init(snapshot: DataSnapshot) {
  70.  
  71. ref = snapshot.ref
  72. studioName = snapshot.key
  73.  
  74. let value = snapshot.value as! NSDictionary
  75.  
  76. studioAddress = value["address"] as? String ?? "---"
  77.  
  78. }
  79. }
Add Comment
Please, Sign In to add comment