黑色UICollectionView

时间:2016-07-31 01:21:10

标签: ios swift uicollectionview

我一直在学习如何使用UICollection视图。我制作了一个新的UICollectionView控制器并将collectionview单元识别为" Cell"在main.storyboard中。每当我点击UICollectionView控制器(通过TapBar控制器)时,我都会出现黑屏。我目前正在使用Firebase导入/导出数据。 谢谢你的帮助!

  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
        }

    }

1 个答案:

答案 0 :(得分:0)

在单元格中放置一个UIView(相对于单元格将其约束设置为0,0,0,0),然后再添加imageView和label。另外,不要忘记遵守代理和数据源。