-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserList View
194 lines (132 loc) · 7.38 KB
/
UserList View
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import UIKit
import Parse
class TableViewController: UITableViewController {
var usernames = [""]
var userIds = [""]
var isFollowing = ["":false]
var refresher:UIRefreshControl!
func refresh() {
print("Refreshed")
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) in
if let users = objects {
self.usernames.removeAll(keepCapacity: true) //to remove empty object in array
self.userIds.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId { //to remove current user from friends list
self.usernames.append(user.username!)
self.userIds.append(user.objectId!)
var query = PFQuery(className: "Followers")
query.whereKey("Following", equalTo: user.objectId!)
query.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)
query.findObjectsInBackgroundWithBlock({ (objects, error) in
if let object = objects {
if objects?.count > 0 {
self.isFollowing[user.objectId!] = true
}else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
self.tableView.reloadData()
self.refresher.endRefreshing()
}
})
}
}
}
}
}) // the whole code above after print refreshed is copied from below
}
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Swipe Down to Refresh")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
refresh()
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) in
if let users = objects {
self.usernames.removeAll(keepCapacity: true) //to remove empty object in array
self.userIds.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId { //to remove current user from friends list
self.usernames.append(user.username!)
self.userIds.append(user.objectId!)
var query = PFQuery(className: "Followers")
query.whereKey("Following", equalTo: user.objectId!)
query.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)
query.findObjectsInBackgroundWithBlock({ (objects, error) in
if let object = objects {
if objects?.count > 0 {
self.isFollowing[user.objectId!] = true
}else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
self.tableView.reloadData()
}
})
}
}
}
}
// print(self.usernames)
// print(self.userIds)
//code is moved to above self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = usernames[indexPath.row]
let followedObjectId = userIds[indexPath.row]
if isFollowing[followedObjectId] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let followedObjectId = userIds[indexPath.row]
if isFollowing[followedObjectId] == false {
isFollowing[followedObjectId]=true
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
var following = PFObject(className: "Followers")
following["Following"] = userIds[indexPath.row]
following["Follower"] = PFUser.currentUser()?.objectId
following.saveInBackground()
} else {
isFollowing[followedObjectId] = false
cell.accessoryType = UITableViewCellAccessoryType.None
var query = PFQuery(className: "Followers")
query.whereKey("Following", equalTo: userIds[indexPath.row])
query.whereKey("Follower", equalTo: (PFUser.currentUser()?.objectId)!)
query.findObjectsInBackgroundWithBlock({ (objects, error) in
if let object = objects {
for object in objects! {
object.deleteInBackground()
}
}
})
}
}