Я пытаюсь получить два UICollectionViews для приложения, над которым я работаю.Я могу получить один, который заполняется Firebase, чтобы показать (imageCollection).К сожалению, я не могу получить второй, чтобы показать (popImageCollection).Все мои магазины там.Я не уверен, что именно я делаю не так.
Вот мой View Controller:
import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage
class ViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var popImageCollection: UICollectionView!
@IBOutlet weak var imageCollection: UICollectionView!
var customImageFlowLayout = CustomImageFlowLayout()
var images = [BlogInsta]()
var popImageArray = [UIImage]()
var dbRef: DatabaseReference!
var dbPopularRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
dbRef = Database.database().reference().child("images")
dbPopularRef = Database.database().reference().child("popular")
loadDB()
loadImages()
customImageFlowLayout = CustomImageFlowLayout()
imageCollection.backgroundColor = .white
popImageCollection.delegate = self as? UICollectionViewDelegate
popImageCollection.dataSource = self
imageCollection.delegate = self as? UICollectionViewDelegate
imageCollection.dataSource = self
self.view.addSubview(popImageCollection)
self.view.addSubview(imageCollection)
}
func loadImages() {
popImageArray.append(UIImage(named: "1")!)
self.popImageCollection.reloadData()
}
func loadDB() {
dbRef.observe(DataEventType.value, with: { (snapshot) in
var newImages = [BlogInsta]()
for BlogInstaSnapshot in snapshot.children {
let blogInstaObject = BlogInsta(snapshot: BlogInstaSnapshot as! DataSnapshot)
newImages.append(blogInstaObject)
}
self.images = newImages
self.imageCollection.reloadData()
})
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.imageCollection {
let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell
let image = images[indexPath.row]
cell.imageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "1"))
return cell
} else {
let cellB = popImageCollection.dequeueReusableCell(withReuseIdentifier: "popCell", for: indexPath) as! PopularCollectionViewCell
let popPhotos = popImageArray[indexPath.row]
cellB.popularImageView.image = popPhotos
return cellB
}
}
}