简体   繁体   中英

Retrieve data of users from parse to show in tableview with swift

This is my code in swift

class UserViewController: UITableViewController {

var userArray: [String] = []

@IBOutlet weak var friendListTableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

       retrieveMessages()

   }
func retrieveMessages() {
    var userArray: [String] = []
    var query:PFQuery = PFQuery(className: "User")
    var currentUser = query.whereKey("username", equalTo: PFUser.currentUser())
    currentUser.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let username:String? = (object as PFObject)["Friends"] as? String
            if username != nil {
                self.userArray.append(username!)
            }
        }
    }
    self.friendListTableView.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 Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return userArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Update - replace as with as!

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = userArray[indexPath.row]

    return cell
}

This is my user's table https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0
This is my current user's friend list in Parse Relation https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0

I've saved current user's friend list with PFRelation in class "User" in column "Friends" and I want to retrieve current user's friend list to show it in tableview but The problem is I can't update tableview to show current user's friend list, It's empty and there's no user list in tableview at all. Is my code correct for this method? If not please help me correct this code. Thank you!

You are updating the table before you receive the list from Parse, try this:

func retrieveMessages() {
    var userArray: [String] = []
    var query:PFQuery = PFQuery(className: "User")
    var currentUser = query.whereKey("username", equalTo: PFUser.currentUser())
    currentUser.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let username:String? = (object as PFObject)["Friends"] as? String
            if username != nil {
                self.userArray.append(username!)
            }
        }
        self.friendListTableView.reloadData()
    }
}

The only difference is that I move the reloadData function inside the completition block so it will happen after the data is returned from parse

You have to call reloadData from inside the findObjectsInBackgroundWithBlock block.

Right now you are calling reloadData before your data is fetched.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM