这是我在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
}
这是我的用户表https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0
这是Parse Relation https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0
我已经在班级"用户"中保存了当前用户的朋友列表和PFRelation。在专栏"朋友"我想检索当前用户的好友列表,以便在tableview中显示,但问题是我无法更新tableview以显示当前用户的好友列表,它是空的,并且&# 39;在tableview中根本没有用户列表。 我的代码对于这种方法是否正确?如果没有,请帮我纠正此代码。 谢谢!
答案 0 :(得分:0)
您在从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()
}
}
唯一的区别是我在completition块中移动了reloadData函数,所以它会在从parse返回数据后发生
答案 1 :(得分:0)
您必须从findObjectsInBackgroundWithBlock块中调用reloadData。
现在,您在获取数据之前调用reloadData。