我有一個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