简体   繁体   English

快速检索用户数据以进行分析以显示在表格视图中

[英]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 这是我的用户表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 这是我在解析关系中当前用户的朋友列表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. 我在“朋友”列的“用户”类中使用PFRelation保存了当前用户的朋友列表,我想检索当前用户的朋友列表以在表视图中显示它,但是问题是我无法更新表视图以显示当前用户的朋友列表,它是空的,并且tableview中根本没有用户列表。 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: 您正在从Parse接收列表之前更新表,请尝试以下操作:

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 唯一的不同是,我将reloadData函数移到了completition块内,因此它将在从解析返回数据之后发生

You have to call reloadData from inside the findObjectsInBackgroundWithBlock block. 您必须从findObjectsInBackgroundWithBlock块内部调用reloadData。

Right now you are calling reloadData before your data is fetched. 现在,您正在提取数据之前调用reloadData。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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