gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:37:57 24 4
gpt4 key购买 nike

我将用户名列和图像列附加到来自解析的两个不同数组。然后我将它们放入 Collection View 中。我预计 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
}

最佳答案

你不应该调用你的查询两次,我想你的 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
}

关于ios - 如何让数组按顺序追加数据库中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33514004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com