2014-11-08 15 views
0

本質上,我建立了一個tableviewcontrollerPFQuery加載信息爲每個單獨的單元格並將其存儲在一個數組中。有兩個調用來解析,一個用於將用戶名存儲到數組中,另一個用於檢查當前用戶是否「跟隨」他們。如何在分析查詢結束時僅重新加載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 

} 

編輯:最初發布的代碼錯誤。

+0

向主線程發送'reloadData'並確保'userNewNames'和'userImagesFiles'是以線程安全的方式訪問的。 – 2014-11-08 05:40:57

+0

我最初發布了錯誤的代碼。請再看一遍!謝謝 – user3483697 2014-11-08 05:58:18

+0

爲了解決這個問題,我用coreData保存整個查詢的結果,然後用NSNotificationCenter調用reloadData()? – user3483697 2014-11-08 06:01:27

回答

2

使用findObjects同步調用並將其放入我們自己的隊列可能有所幫助。

// queue is declared as an instance property 
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL) 

var query = PFUser.query() 

dispatch_async(queue) { 
    dispatch_async(dispatch_get_main_queue()) { 
     self.users.removeAll(keepCapacity: true) // users is an array declared globally 
    } 

    for user in query.findObjects() as [PFUser] { 
     var isFollowing = false 

     if user.username != PFUser.currentUser().username { 
      dispatch_async(dispatch_get_main_queue()) { 
       self.users.append(user.username) 
      } 

      var query = PFQuery(className:"followers") 

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

      // call findObjects synchronously 
      if let objects = query.findObjects() { 
       if !objects.isEmpty { 
        isFollowing = true 
       } 

       dispatch_async(dispatch_get_main_queue()) { 
        self.following.append(isFollowing) 
       } 
      } 
     } 
    } 

    // notify table view to reload 
    dispatch_async(dispatch_get_main_queue()) { 
     self.tableView.reloadData() 
    } 
} 

您可以創建一個UITableViewCell子類,並覆蓋其prepareForReuse重置accessoryType

class MyTableView: UITableViewCell { 
    override func prepareForReuse() { 
     accessoryType = .None 
    } 
} 

在您的代碼中,您沒有處理following[indexPath.row] != true的情況。如果你不想子類UITableViewCell,你也可以在那裏重置對勾。

+0

謝謝Swipesight。只是搜索「dispatch_async(dispatch_get_main_queue())」,並且被混淆了。它是否在查詢塊內的後臺異步運行append()。然後,當以前的dispatch_async(dispatch_get_main_queue())完成時,僅調用dispatch_async(dispatch_get_main_queue())纔會再次運行reloadData()?在查詢完成之前它不會運行reloadData嗎?感謝您幫助初學者 – user3483697 2014-11-08 07:37:37

+0

實際上,您的代碼由於某種原因使得每個「isFollowing」都是真實的。:( – user3483697 2014-11-08 07:54:00

+0

當狀態欄中的活動指示器無限運行時,表格視圖現在爲空。再次,請欣賞目前的幫助 – user3483697 2014-11-08 10:30:10

相關問題