2016-11-20 57 views
0

我对SO非常陌生。我正尝试使用Firebase数据库中的数据填充集合视图。从Firebase获取数据的用户var会随所有数据回来。我试图找出为什么我的UICollectionView回来Nil。我觉得崩溃发生在这条线:将UICollectionView连接到Firebase

self.usersCollectionView.reloadData() 

和错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value

它我collectionView无功即传回了nil

我已将单元类连接到单元格。

import UIKit 
import Firebase 

class UserCollectionController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    var cellIdentifier = "User Cell" 

    var users = [User]() 

    @IBOutlet weak var usersCollectionView: UICollectionView! 

    func createAlert(title: String, message: String) { 

     let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 

     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { (action) in 

      self.dismiss(animated: true, completion: nil) 

     })) 

     self.present(alert, animated: true, completion: nil) 

    } 

    func fetchUser() { 

     FIRDatabase.database().reference().child("users").observe(FIRDataEventType.childAdded, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 

       let user = User() 

       user.name = dictionary["name"] as! String? 
       user.profileImageURL = dictionary["profileImageURL"] as! String? 

       self.users.append(user) 

       DispatchQueue.main.async(execute: { 

        self.usersCollectionView.reloadData() 

       }) 


      } 

      print(snapshot) 

     }, withCancel: nil) 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     fetchUser() 

    } 


    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return users.count 
    } 

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! UserCollectionViewCell 

     let cellUsers = users[indexPath.item] 

     //cell.users = users[indexPath.item] 

     cell.usersNameLabel.text = cellUsers.name 

     if let profileImageURL = cellUsers.profileImageURL { 


      cell.usersImageView.loadImageUsingCacheWithUrlString(urlString: profileImageURL) 
     } 

     return cell 
    } 


    public func numberOfSections(in collectionView: UICollectionView) -> Int 
    { 
     return 1 
    } 
} 

class UserCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var usersImageView: UIImageView! 

    @IBOutlet weak var usersNameLabel: UILabel! 
    /* 
    // MARK: - Public API 
    var users: User! { 

     didSet { 

      updateUI() 

     } 

    } 

    // MARK: - Private 

    //@IBOutlet weak var featuredImageView: UIImageView! 
    //@IBOutlet weak var interestTitleLabel: UILabel! 

    func updateUI() { 

     if let profileImage = users.profileImageURL { 

      self.usersImageView?.loadImageUsingCacheWithUrlString(urlString: profileImage) 

     } 

     usersNameLabel?.text! = users.name! 

    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     self.layer.cornerRadius = 10.0 
     self.clipsToBounds = true 
    } 

    */ 

} 

这是我User VAR:

import UIKit 

class User: NSObject { 

    var name: String? 
    var email: String? 
    var password: String? 
    var profileImageURL: String? 

} 

如果对我的代码有任何疑问,或者如果您需要更多的信息。请不要犹豫,问。

+0

你能否尝试看看当你改变FIRDataEventType.childAdded到FIRDataEventType.value会发生什么? –

+0

谢谢Jordi。我的代码实际上工作。它的我的UICollectionView不会显示细节的细节。对不起,我认为这是连接到数据库不工作。我做了一个打印(用户),并在控制台上打印了用户。 –

+0

看来你的collectionView没有正确创建。你如何将它连接到故事板? –

回答

0

看起来好像你的collectionView没有正确创建。你如何将它连接到故事板?

您正试图在正确设置之前重新加载collectionView。

+0

我也是新人,你如何将它连接到故事板? –