如何让数组按顺序追加数据库中的对象?

时间:2015-11-04 04:57:07

标签: ios swift parse-platform

我将username列和image列附加到parse的两个不同数组中。然后我将它们放入集合视图中。我期待nameArray中的用户名对应于imageArray,但大多数时候它们的顺序错误。如何让它们以正确的顺序附加到数组中?即,用户1具有图片1,用户2具有图片2.用户名阵列= [用户1,用户2]。图像阵列= [图片1,图片2]。

func getFriendPicandName(){

    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)

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

        }
    }



    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)

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



    })


   }


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrayOfFriendsNames.count
}
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
}

2 个答案:

答案 0 :(得分:2)

你不应该两次调用你的查询,我想你的for循环看起来像这样:

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!) // Add image here
        print(self.arrayOfFriends)

    }

    self.arrayOfFriendsNames.append(object.valueForKey("username") as! String) // Add Name here
}

答案 1 :(得分:0)

我不太了解斯威夫特,但我把两个功能合二为一......我希望它能帮到你......

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

            var objectIDs = objects as! [PFObject]
            for i in 0...(objectIDs.count){
                self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
                print(self.arrayOfFriendsNames)

            }

            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)

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

        }
    }
}