gpt4 book ai didi

ios - 使用解析来检索并在 TableView 中显示用户的对象,但 TableView 不会更新

转载 作者:行者123 更新时间:2023-11-30 14:19:32 25 4
gpt4 key购买 nike

这是我的 swift 代码

class UserViewController: UITableViewController {

var userArray: [String] = []

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

retrieveMessages()
self.friendListTableView.reloadData()


}
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
if usernames != nil {
self.userArray.append(usernames!)
}
}

}

}
}
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
}

我的 UITableView 不会更新。我已将 self.friendListTableView.reloadData() 的位置从retrieveMessage 函数内部更改为 viewDidLoad 方法内部,并且表仍然是空的。我相信我在 retrieveMessage() 中的方法不知何故不正确,可能在 for object in objects { ... } 内。

这是我的“User”类的解析屏幕截图 https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0
这是我当前用户的关系表,由 PFRelation 方法保存 https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0

感谢您的帮助,如果您需要任何其他信息,请告诉我。谢谢

最佳答案

您似乎忘记在填充数组后在表上调用 reloadData

在此之后:

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

你需要这个:

dispatch_async(dispatch_get_main_queue()) {
self.friendListTableView.reloadData()
}

关于ios - 使用解析来检索并在 TableView 中显示用户的对象,但 TableView 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com