我有一个collectionview,我有一个连接到解析的字符串username的数组,但是当我运行该程序时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
}
设置数据源和代表collectionview的。 – pkc456
我已经完成了。 – xxtmanxx