Reputation: 1160
I'm quite new to SO. I'm trying to populate a collection view with data from a Firebase Database. The Users var that fetches data from Firebase comes back right with all the data. What I'm trying to figure out why my UICollectionView
comes back with Nil
. I think the crash happens in this line:
self.usersCollectionView.reloadData()
and the error is:
fatal error: unexpectedly found nil while unwrapping an Optional value
Its my collectionView
var that's returning a nil
.
I have connected the cell class to the cell.
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
}
*/
}
This is my User
var:
import UIKit
class User: NSObject {
var name: String?
var email: String?
var password: String?
var profileImageURL: String?
}
If there is any questions about my code or if you need more info. Please don't hesitate to ask.
Upvotes: 2
Views: 1091
Reputation: 1588
It seems like your collectionView is not created properly. How have you connected it in storyboard?
You're trying to reload the collectionView before it's been properly set.
Upvotes: 1