gpt4 book ai didi

ios - 将 UICollectionView 连接到 Firebase

转载 作者:行者123 更新时间:2023-11-28 06:29:41 24 4
gpt4 key购买 nike

我对 SO 很陌生。我正在尝试使用 Firebase 数据库中的数据填充 Collection View 。从 Firebase 获取数据的 Users var 返回所有数据。我想弄清楚为什么我的 UICollectionView 返回 Nil。我认为崩溃发生在这一行:

self.usersCollectionView.reloadData()

错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value

返回 nil 的是我的 collectionView 变量。

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

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
}

*/

}

这是我的用户 变量:

import UIKit

class User: NSObject {

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

}

如果对我的代码有任何疑问或者您需要更多信息。请随时提问。

最佳答案

您的 collectionView 似乎未正确创建。你是如何在 Storyboard中连接它的?

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

关于ios - 将 UICollectionView 连接到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com