用户的朋友列表tableview为空,解析查询未返回任何数据

时间:2015-06-04 10:02:18

标签: ios uitableview swift parse-platform pfobject

这是我在swift中的代码

appModule

我已经在班级"用户"中保存了当前用户的朋友列表和PFRelation。在专栏"朋友",这里是我如何保存它

class UserViewController: UITableViewController {

var userArray: NSMutableArray = []

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

       retrieveMessages()

   }
func retrieveMessages() {
    var query = PFUser.query()
    if let username = PFUser.currentUser().username {
        query.whereKey("username", equalTo: username)
        query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let usernames:String? = (object as PFObject)["Friends"] as? String
            println(usernames) // It prints nil
            if usernames != nil {
                self.userArray.addObject(usernames!)
            }
        }
                dispatch_async(dispatch_get_main_queue()) {
                    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] as? String

    return cell
}

现在我想检索当前用户的好友列表以在tableview中显示它但问题是我无法更新tableview以显示当前用户的好友列表,它是空的,桌面视图中根本没有用户列表。我的代码对于这种方法是否正确?如果没有,请帮我纠正此代码

这里是我的用户在解析中的类表的屏幕截图 Parse user list

这是我当前用户在解析中的朋友的屏幕截图,其中使用PFRelation保存 Parse friend user list 感谢您的任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

我暂时没有使用Parse,但是如果我没有弄错的话,第一个查询(在第一个代码块中)没有正确构建。应该是这样的:

 var query:PFQuery = PFQuery(className: "User")
 if let username = PFUser.currentUser().username {
   query.whereKey("username", equalTo: username)
   query.findObjectsInBackgroundWithBlock {....}
 }

这里的区别在于您查询当前用户的用户名而不是对象本身。这有帮助吗?

答案 1 :(得分:0)

将数组更改为NSMutableArray,并检查它是否附加数据。

var userArray: NSMutableArray = []

在retrieveMessages中尝试查询Parse" User"表使用

var query = PFUser.query()

然后你没有将用户名传递给String 试试

query.whereKey("username", equalTo: PFUser.currentUser().username)