Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import UIKit
  2. import FirebaseStorage
  3. import FirebaseFirestore
  4.  
  5. struct Posts {
  6. var caption:String
  7. }
  8.  
  9. class LentaViewController: UIViewController {
  10. @IBOutlet weak var tableView: UITableView!
  11. var posts = [Posts]()
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. loadPosts()
  16.  
  17. DispatchQueue.main.async {
  18. self.tableView.reloadData()
  19. }
  20. }
  21.  
  22. func loadPosts() {
  23. let dbUsers = Firestore.firestore().collection("posts")
  24. dbUsers.addSnapshotListener { (querySnapshot, error) in
  25. if let error = error {
  26. print("(error.localizedDescription)")
  27. } else {
  28. for document in (querySnapshot?.documents)! {
  29. if let Caption = document.data()["caption"] as? String {
  30. print(Caption)
  31. var post = Posts(caption: "")
  32. post.caption = Caption
  33.  
  34. self.posts.append(post)
  35. }
  36.  
  37. }
  38. DispatchQueue.main.async
  39. {
  40. self.tableView.reloadData()
  41. }
  42. print(self.posts)
  43. }
  44. }
  45. }
  46. }
  47.  
  48. extension LentaViewController: UITableViewDataSource {
  49. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. return posts.count
  51. }
  52.  
  53. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  54.  
  55. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  56. let post = posts[indexPath.row]
  57. cell.textLabel?.text = post.caption
  58. return cell
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement