gpt4 book ai didi

ios - 为什么 CollectionView 上没有显示任何内容?

转载 作者:行者123 更新时间:2023-11-30 13:58:10 25 4
gpt4 key购买 nike

我有一个collectionview,并且有一个带有字符串用户名的数组,该数组连接到解析,但是当我运行程序时,collectionview不显示任何内容。我通过手动将值放入数组中对其进行了硬编码,并且它起作用了,但是一旦我从解析中检索字符串,它就不起作用了。我该如何解决这个问题?

var arrayOfFriendsNames = [String]()
var arrayOfFriendsTest: [String] = ["fire.png, fire.png, fire.png"]
override func viewDidLoad() {
super.viewDidLoad()

var query = PFQuery(className: "_User")
query.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var objectIDs = objects as! [PFObject]

for i in 0...objectIDs.count-1{
self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
print(self.arrayOfFriendsNames)
}
})
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.row]
cell.friendpic.image = UIImage(named: arrayOfFriendsTest[indexPath.row])
cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
cell.friendpic.clipsToBounds = true

return cell
}

最佳答案

从数据库获取数据后尝试重新加载

    query.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var objectIDs = objects as! [PFObject]

for i in 0...objectIDs.count-1{
self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
print(self.arrayOfFriendsNames)
}

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

})

关于ios - 为什么 CollectionView 上没有显示任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337708/

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