2016-07-31 11 views
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 
     } 

    } 
+0

'sizeForItemAtIndexpath'というセルのサイズは設定しません。また、あなたのコードには、 'elf'ではなく' self'があります。 'userImageArray'にアイテムがあるかどうかを確認してください。 – Brandon

+0

イメージ配列にURL値があり、クラッシュすることなくプログラムを実行できるので、「elf」はないと思います。私はストーリーボードを通してセルのサイズを作りましたが、CollectionViewControllerの色に関係なく関係していますか? –

+0

あなたは基本的なデバッグを実行しないというあなたの問題です。 [AppleのUIViewControllerデモアプリ](https://developer.apple.com/library/prerelease/content/samplecode/CollectionView-Simple/Introduction/Intro.html)でダウンロードして再生してください。 – Jeff

答えて

0

UIViewをセルに配置し(セルに対して制約を0,0,0,0に設定)、後でimageViewとラベルを追加します。また、代理人とデータソースを遵守することを忘れないでください。

関連する問題