Я использую collectionView внутри tableView для того чтобы в каждой из ячеек tableView можно было скролить изображения. Изображения подгружаю из firebase. Ветка firebase выглядит так:
{
"Дома" : {
"Дом1" : {
"address" : "address1",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image2" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"name" : "Home1"
},
"Дом2" : {
"address" : "address2",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image2" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image3" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"name" : "Home2"
},
"Дом3" : {
"address" : "address3",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image2" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image3" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image4" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image5" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"name" : "Home3"
}
}
}
И когда я подгружаю изображения то у меня создается максимальное количество изображений, т.е. 5 ячеек в каждом из collectionView. Но на примере дом1 и дом2 у меня остаются пустые ячейки до которых можно скролить. Как мне убрать/удалить/скрыть эти пустые ячейки в дом1 и дом2? Соответственно домов будет около 500 штук, а не 3 дома как я указал в firebase. Я их буду создавать в firebase чтобы они автоматически подгружались в приложение. Мой код: ViewController (tableView):
@IBOutlet weak var tableView: UITableView!
var nameAddress = [Film]()
var nameAddressRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
nameAddressRef = Database.database().reference(withPath: "Дома")
loadNameAddress()
}
func loadNameAddress() {
nameAddressRef.observe(DataEventType.value, with: { (snapshot) in
var newImages = [Film]()
for studioImagesSnapshot in snapshot.children {
let studioImageObject = Film(snapshot: studioImagesSnapshot as! DataSnapshot)
newImages.append(studioImageObject)
}
self.nameAddress = newImages
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return nameAddress.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryRow
cell.nameLabel.text = nameAddress[indexPath.section].name
cell.images = [nameAddress[indexPath.section].image1,
nameAddress[indexPath.section].image2,
nameAddress[indexPath.section].image3,
nameAddress[indexPath.section].image4,
nameAddress[indexPath.section].image5]
return cell
}
}
CategoryRow (collectionView):
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
var images = [String]()
override func prepareForReuse() {
self.nameLabel.text = nil
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
extension CategoryRow: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.sd_setImage(with: URL(string: images[indexPath.item]))
return cell
}
}
И структура с помощью которой я подгружаю изображения:
struct Film {
let ref: DatabaseReference?
var name: String!
var image: String!
var image1: String!
var image2: String!
var image3: String!
var image4: String!
var image5: String!
init(name: String) {
self.name = name
self.ref = nil
}
init(snapshot: DataSnapshot) {
ref = snapshot.ref
let snapshotValue = snapshot.value as! NSDictionary
name = snapshotValue["name"] as? String ?? ""
image = snapshotValue["image"] as? String ?? ""
image1 = snapshotValue["image1"] as? String ?? ""
image2 = snapshotValue["image2"] as? String ?? ""
image3 = snapshotValue["image3"] as? String ?? ""
image4 = snapshotValue["image4"] as? String ?? ""
image5 = snapshotValue["image5"] as? String ?? ""
}
}