简体   繁体   中英

Why is the collection view not reloading?

I am populating an array from data in parse. The array is being populated, but when I add it to the collection view and refresh the view, nothing shows up. How do I fix this? Is there a certain place where I have to refresh the collection view? The array is populated: 在此处输入图片说明

   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
}

You are using arrayOfFriendsNames . So try to reload like this

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

           }

    })

}

You need to be reloading your collection view after your closure is finished executing when you've retrieved the data from Parse, not in 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM