本質上,我建立了一個tableviewcontroller
與PFQuery
加載信息爲每個單獨的單元格並將其存儲在一個數組中。有兩個調用來解析,一個用於將用戶名存儲到數組中,另一個用於檢查當前用戶是否「跟隨」他們。如何在分析查詢結束時僅重新加載tableview一次?
var query = PFUser.query()
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
self.users.removeAll(keepCapacity: true) // users is an array declared globally
for object in objects {
var user:PFUser = object as PFUser
var isFollowing:Bool
if user.username != PFUser.currentUser().username {
self.users.append(user.username)
isFollowing = false
var query = PFQuery(className:"followers")
query.whereKey("follower", equalTo:PFUser.currentUser().username)
query.whereKey("following", equalTo:user.username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
isFollowing = true
}
self.following.append(isFollowing)
self.tableView.reloadData()
} else {
// Log details of the failure
println(error)
}
}
}
}
}
如果我在另一個地方撥打reloadData()
,表格將無法正確顯示。這使得我的表加載效率低下,因爲表重新加載了每個「正在跟隨」變量的檢查。我將如何重新加載一次?在所有「正在追隨」變量已被追加後?你能否一步一步地概述解決方案,以便我能夠獲得普遍的方向感和學習內容。
EDIT2:
在上述我查詢來檢查currentUser是以下哪些用戶的代碼。如果他們正在關注它們,我會在「跟隨」數組中添加「true」。下面的代碼是我正在做的checkview tableviewcells,以表明他們正在跟隨其他用戶。使用我的原始代碼,所有單元格在上下滾動幾次後都會被選中,爲什麼?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
if following.count > indexPath.row {
if following[indexPath.row] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
cell.textLabel.text = users[indexPath.row]
return cell
}
編輯:最初發布的代碼錯誤。
向主線程發送'reloadData'並確保'userNewNames'和'userImagesFiles'是以線程安全的方式訪問的。 – 2014-11-08 05:40:57
我最初發布了錯誤的代碼。請再看一遍!謝謝 – user3483697 2014-11-08 05:58:18
爲了解決這個問題,我用coreData保存整個查詢的結果,然後用NSNotificationCenter調用reloadData()? – user3483697 2014-11-08 06:01:27