2015-11-04 102 views
1

我從解析中的數據填充數組。該數組正在填充,但是當我將它添加到集合視圖並刷新視圖時,什麼也沒有顯示。我該如何解決?是否有某個地方需要刷新收藏視圖?填充該數組:enter image description here爲什麼收藏視圖不能重新加載?

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated); 

    getFriendName() 
    getFriendPic() 


} 

func getFriendPic(){ 

    let imagequery = PFQuery(className: "_User") 
    imagequery.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in 
     // for object in objects!{ 
      var user = PFUser.currentUser() 
      let relation = user!.relationForKey("Friendship") 
      relation.query()!.findObjectsInBackgroundWithBlock { 
       (objects: [AnyObject]?, error: NSError?) -> Void in 
       for object in objects!{ 
       let userPic = object["ProPic"] as! PFFile 
       userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in 
        if(error == nil){ 
         let image = UIImage(data: imageData!) 
         self.arrayOfFriends.append(image!) 
         print(self.arrayOfFriends) 

        } 
        self.collectionView.reloadData() 
      }) 

      } 

     } 
    } 
} 

func getFriendName(){ 
    var query = PFQuery(className: "_User") 
    query.findObjectsInBackgroundWithBlock({ 
     (objects: [AnyObject]?, error: NSError?) -> Void in 
     var user = PFUser.currentUser() 
      let relations = user!.relationForKey("Friendship") 
      relations.query()!.findObjectsInBackgroundWithBlock{ 
       (objects: [AnyObject]?, error: NSError?) -> Void in 
       var objectIDs = objects as! [PFObject] 
       for i in 0...(objectIDs.count){ 
       self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String) 
       print(self.arrayOfFriendsNames) 

      } 
      self.collectionView.reloadData() 
     } 



    }) 

} 



func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView 




    cell.friendname.text = arrayOfFriendsNames[indexPath.item] 
    cell.friendpic.image = arrayOfFriends[indexPath.item] 
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2; 
    cell.friendpic.clipsToBounds = true 


    return cell 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return arrayOfFriendsNames.count 
} 
+0

,你回到小區 – anhtu

回答

1

您正在使用後arrayOfFriendsNames。所以儘量reload這樣

func getFriendName(){ 
    var query = PFQuery(className: "_User") 
    query.findObjectsInBackgroundWithBlock({ 
     (objects: [AnyObject]?, error: NSError?) -> Void in 
      var user = PFUser.currentUser() 
      let relations = user!.relationForKey("Friendship") 
      relations.query()!.findObjectsInBackgroundWithBlock{ 
       (objects: [AnyObject]?, error: NSError?) -> Void in 
       var objectIDs = objects as! [PFObject] 
       for i in 0...(objectIDs.count){ 
        self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String) 
        print(self.arrayOfFriendsNames) 

       } 

      /// here 
      dispatch_async(dispatch_get_main_queue()) { 
       self.collectionView.reloadData() 
      } 

      } 

    }) 

} 
+0

我繼續的NUM請張貼更多的代碼,並把getfriendpic和名稱在視圖中會出現(看更新的代碼),但有時它加載,有時收集視圖是空白的,有時會崩潰。 – xxtmanxx

+0

我知道它,但圖像的順序不同。我認爲這是因爲它被以錯誤的順序保存。無論如何要按順序保存它?它有時還會出現。 – xxtmanxx

+0

@xxtmanxx你應該檢查你的數據庫。但是第一個,你應該把getFriendName()和getFriendPic()混合到1個函數中。也許你可以發佈另一個問題。 – anhtu

0

您必須重裝集合視圖您關閉執行完畢,當你從獲取分析數據,而不是在cellForRowAtIndexPath

query.findObjectsInBackgroundWithBlock({ 
    (objects: [AnyObject]?, error: NSError?) -> Void in 
    var user = PFUser.currentUser() 
     let relations = user!.relationForKey("Friendship") 
     relations.query()!.findObjectsInBackgroundWithBlock{ 
      (objects: [AnyObject]?, error: NSError?) -> Void in 
      var objectIDs = objects as! [PFObject] 
      for i in 0...(objectIDs.count){ 
      self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String) 
      print(self.arrayOfFriendsNames) 

     } 
    } 
}) 
self.collectionView.reloadData() 
相關問題