-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedViewController.swift
74 lines (54 loc) · 2.03 KB
/
FeedViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// FeedViewController.swift
// Parstagram
//
// Created by Erin DeLong on 5/4/21.
//
import UIKit
import Parse
import AlamofireImage
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var posts = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let query = PFQuery(className: "Posts")
query.includeKey("author")
query.limit = 20
query.findObjectsInBackground { (posts, error) in
if posts != nil {
self.posts = posts!
self.tableView.reloadData()
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! PostCell
let post = posts[indexPath.row]
let user = post["author"] as! PFUser
cell.usernameLabel.text = user.username
cell.captionLabel.text = post["caption"] as! String
let imageFile = post["image"] as! PFFileObject
let urlString = imageFile.url!
let url = URL(string: urlString)!
cell.photoView.af_setImage(withURL: url)
return cell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}