2015-11-04 98 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() 
相关问题