Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import FirebaseStorage
- import FirebaseFirestore
- struct Posts {
- var caption:String
- }
- class LentaViewController: UIViewController {
- @IBOutlet weak var tableView: UITableView!
- var posts = [Posts]()
- override func viewDidLoad() {
- super.viewDidLoad()
- loadPosts()
- DispatchQueue.main.async {
- self.tableView.reloadData()
- }
- }
- func loadPosts() {
- let dbUsers = Firestore.firestore().collection("posts")
- dbUsers.addSnapshotListener { (querySnapshot, error) in
- if let error = error {
- print("(error.localizedDescription)")
- } else {
- for document in (querySnapshot?.documents)! {
- if let Caption = document.data()["caption"] as? String {
- print(Caption)
- var post = Posts(caption: "")
- post.caption = Caption
- self.posts.append(post)
- }
- }
- DispatchQueue.main.async
- {
- self.tableView.reloadData()
- }
- print(self.posts)
- }
- }
- }
- }
- extension LentaViewController: UITableViewDataSource {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return posts.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- let post = posts[indexPath.row]
- cell.textLabel?.text = post.caption
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement