我正在尝试使用 Firebase 数据库中的数据填充我的 CollectionView。数据似乎很好,但没有任何反应,collectionView 单元格没有填充。我做错了什么???
这是我试图放入收藏视图的部分:
struct PlantsCollection: Hashable, Codable {
let name: String
let plants: [Plant]
var identifier = UUID().uuidString
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
static func ==(lhs: PlantsCollection, rhs: PlantsCollection) -> Bool {
return lhs.identifier == rhs.identifier
}
enum CodingKeys: String, CodingKey {
case name = "name"
case plants = "plants"
case identifier = "identifier"
}}
然后反对
final class Plant: NSObject, Codable {
let name: String
let lifeTime: Float
let plantFamily: String
let video: String
let plantDescription: String
let imageUrl: String
init(name: String, lifeTime: Float, plantFamily: String, video: String, plantDescription: String, imageUrl: String) {
self.name = name
self.lifeTime = lifeTime
self.plantFamily = plantFamily
self.video = video
self.plantDescription = plantDescription
self.imageUrl = imageUrl
}
enum CodingKeys: String, CodingKey {
case name = "name"
case lifeTime = "lifeTime"
case plantDescription = "description"
case plantFamily = "plantFamily"
case video = "video"
case imageUrl = "imageUrl"
}}
这是我的 ViewController 代码
final class ViewController: UIViewController, UITextFieldDelegate{
var ref: DatabaseReference!
var data: [DataSnapshot] = []
fileprivate var _refHandle: DatabaseHandle!
var storageRef: StorageReference!
var remoteConfig: RemoteConfig!
@IBOutlet weak var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<PlantsCollection, Plant>!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private func setupView() {
self.title = "Library"
collectionView.collectionViewLayout = configureCollectionViewLayout()
configureDatabase()
configureDataSource()
configureSnapshot()
}
}
// MARK: - Collection View -
extension ViewController {
func configureCollectionViewLayout() -> UICollectionViewCompositionalLayout {
let sectionProvider = { (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) ->
NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.6), heightDimension: .fractionalHeight(0.3))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
section.interGroupSpacing = 10
return section
}
return UICollectionViewCompositionalLayout(sectionProvider: sectionProvider)
}
func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource<PlantsCollection, Plant>(collectionView: collectionView) {
(collectionView, indexPath, plant) -> UICollectionViewCell? in
guard let cell =
collectionView.dequeueReusableCell(withReuseIdentifier: PlantCell.reuseIdentifier, for: indexPath) as? PlantCell else {
fatalError("Cannot create new cell")
}
cell.titleLabel.text = plant.name
return cell
}
}
func configureSnapshot() {
var currentSnapshot = NSDiffableDataSourceSnapshot<PlantsCollection, Plant>()
ref.child("plantsCollection").observe(.value, with: { snapshot in
let jsonData = try! JSONSerialization.data(withJSONObject: snapshot.value!, options: .prettyPrinted)
let myRealData = try! JSONDecoder().decode(PlantsCollection.self, from: jsonData)
currentSnapshot.appendSections([myRealData])
currentSnapshot.appendItems(myRealData.plants)
})
self.dataSource.apply(currentSnapshot, animatingDifferences: false)
}
}
// MARK: "Configure Database"
extension ViewController {
func configureDatabase() {
ref = Database.database().reference()
// Listen for new messages in the Firebase database\
_refHandle = self.ref.child("plantsCollection").observe(.childAdded, with: {
[weak self] (snapshot) -> Void in
guard let strongSelf = self else { return }
strongSelf.data.append(snapshot)
})
}
}
我的 Firebase 中的数据是
{
"plantsCollection" : {
"identifier" : "id",
"name" : "MoyaColeccia",
"plants" : [ {
"description" : "rozi novoznye",
"imageUrl" : "someURL",
"lifeTime" : 13.0,
"name" : "ROZA",
"plantFamily" : "ROZNIE",
"video" : "VSTAV URL SUDA"
}, {
"description" : "Pey Aloe budish molodech",
"imageUrl" : "someURL",
"lifeTime" : 14.1,
"name" : "ALOE",
"plantFamily" : "Aloynie",
"video" : "VSTAV URL SUDA"
}, {
"description" : "Dub vsevmu golova",
"imageUrl" : "someURL",
"lifeTime" : 13.4,
"name" : "DUB",
"plantFamily" : "Duby",
"video" : "VSTAV URL SUDA"
} ]
}
}
请帮忙,卡住了...