I'm trying to make a page to show who a user is following in a table. Everything seems pretty clear to me, but for some reasons this code is not loading my data. The following is my following page controller :
import Foundation import Parse
class FollowingController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var profileId : String = String()
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
@IBOutlet weak var topTitleBar: UIImageView!
@IBOutlet weak var mainBox: UIImageView!
@IBOutlet var tableView: UITableView!
var resultData:NSMutableArray = NSMutableArray()
func loadData(){
resultData.removeAllObjects()
loadingIndicator.hidden = false
loadingIndicator.startAnimating()
var counter = 0
let followingQuery = PFQuery(className: "Followers")
followingQuery.whereKey("following", equalTo: profileId)
followingQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
let userQuery = PFUser.query()
userQuery?.whereKey("objectId", equalTo: object["followed"] as! String)
userQuery?.findObjectsInBackgroundWithBlock({ (users, error ) -> Void in
if let users = users {
for user in users{
let followed = user as! PFUser
self.resultData.addObject(followed)
counter++
}
}
})
}
}
let array:NSArray = self.resultData.reverseObjectEnumerator().allObjects
self.resultData = NSMutableArray(array: array)
self.tableView.reloadData()
if counter == 0 {
//Handle no results
}
}
loadingIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
self.loadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return resultData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:FollowingCell = tableView.dequeueReusableCellWithIdentifier("FollowingCell", forIndexPath: indexPath) as! FollowingCell
let user:PFUser = self.resultData.objectAtIndex(indexPath.row) as! PFUser
let name = (user.objectForKey("firstName") as! String) + " " + (user.objectForKey("lastName") as! String)
cell.name.text = name
if let profilePicture = user.objectForKey("profilePicture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock({ (data, error) -> Void in
if error == nil {
let profileImage = UIImage(data: data!)
cell.profilePicture.image = profileImage
}
})
}
return cell
}
override func viewDidLayoutSubviews() {
}
@IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: {});
}
}
I created a custom class for my cell and called it FollowingCell. All the outlets are connected to the variables in the class as well as the table datasource & delegate to the ViewController! If you spot anything that might be causing this table not to display the data please let me know. Thanks.
EDIT : Found out the problem is that even though I load the resultData array in the for loop for each user, outside the findObjectInBackgroundWithBlock call the resultData count returns 0. So it's like if it's not actually updating the array. Anybody knows why could that be?
You forgot to reload the tableview
mytableview.reloadData()
everytime you load the data you have to reload the full table view or append cells to it..
And check the cell identifiers declared correctly in storyboard
You might forget to set identifier for your Cell from Storyboard as "FollowingCell", double check it. Also reload the tableView.
I found the problem, there was a problem when I reloaded the data outside the second query. Reloading the data inside the second query call and outside the inner one worked!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.