0
私はUICollectionビューの使い方を学んでいます。新しいUICollectionView Controllerを作成し、main.storyboardのcollectionviewセルを "Cell"として指定しました。 UICollectionViewコントローラー(TapBarコントローラー経由)をタップするたびに黒い画面が表示されます。現在、Firebaseを使用してデータをインポート/エクスポートしています。 ありがとうございます!黒UICollectionView
class UserCollectionViewController: UICollectionViewController {
var databaseRef = FIRDatabase.database().reference()
var usersDict = NSDictionary?()
var userNameArray = [String]()
var userImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.databaseRef.child("user_profile").observeEventType(.Value, withBlock :{
(snapshot) in
self.usersDict = snapshot.value as? NSDictionary
for(userId,details) in self.usersDict! {
let img = details.objectForKey("profile_pic_small") as! String
let name = details.objectForKey("name") as! String
let firstName = name.componentsSeparatedByString(" ")[0]
self.userImageArray.append(img)
self.userNameArray.append(firstName)
self.collectionView?.reloadData()
}
})
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.userImageArray.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
return cell
let imageUrl = NSURL(string: userImageArray[indexPath.row])
let imageData = NSData(contentsOfURL: imageUrl!)
cell.userImage.image = UIImage(data: imageData!)
cell.userName.text = userNameArray[indexPath.row]
return cell
}
}
'sizeForItemAtIndexpath'というセルのサイズは設定しません。また、あなたのコードには、 'elf'ではなく' self'があります。 'userImageArray'にアイテムがあるかどうかを確認してください。 – Brandon
イメージ配列にURL値があり、クラッシュすることなくプログラムを実行できるので、「elf」はないと思います。私はストーリーボードを通してセルのサイズを作りましたが、CollectionViewControllerの色に関係なく関係していますか? –
あなたは基本的なデバッグを実行しないというあなたの問題です。 [AppleのUIViewControllerデモアプリ](https://developer.apple.com/library/prerelease/content/samplecode/CollectionView-Simple/Introduction/Intro.html)でダウンロードして再生してください。 – Jeff