Guest User

Untitled

a guest
Jan 16th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3.  
  4. class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  5. @IBOutlet weak var tableView: UITableView!
  6. var items = [String]()
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
  12. tableView.delegate = self
  13. tableView.dataSource = self
  14.  
  15. let db = Firestore.firestore()
  16. db.collection("items").getDocuments() { (querySnapshot, err) in
  17. if let err = err {
  18. print("Error getting documents: \(err)")
  19. } else {
  20. for document in querySnapshot!.documents {
  21. if let name = document.data()["name"] as? String {
  22. self.items.append(name)
  23. }
  24. }
  25. self.tableView.reloadData()
  26. }
  27. }
  28. }
  29.  
  30. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  31. return 1
  32. }
  33.  
  34. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  35. return items.count
  36. }
  37.  
  38. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  39. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
  40. cell.textLabel?.text = items[indexPath.row]
  41. return cell
  42. }
  43. }
Add Comment
Please, Sign In to add comment