I'm trying to save a list of image url's to an empty array of strings to then show in a collection view. I'm having trouble looping through the dictionary to store the URLs.
I get the Firebase data in the EncounterTableViewController.swift , then have another detailed view controller EncounterDetailViewController.swift that has an EncounterCollectionViewCell.swift
Encounter.swift
class Encounter {
...
...
var images: [String] = []
}
EncounterTableViewController.swift
func showAllEncounters() {
// Firebase tableview data
FIRDatabase.database().reference().child("encounters").observeSingleEvent(of: .value, with: { (snapshot) in
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let restDict = rest.value as? [String: Any] else { continue }
let encounter = Encounter()
...
...
let mediaDict = restDict["media"] as! [[String:Any]]
// need to find nested images and set them to encounter.images here
self.encounters.append(encounter)
self.tableView.reloadData()
}
})
}
EncounterDetailViewController.swift
private let reuseIdentifier = "imageCell"
class EncounterDetailViewController: UIViewController,
UICollectionViewDataSource, UICollectionViewDelegate {
// MARK: - Properties
var selectedEncounter: Encounter?
// MARK: - View did load
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (selectedEncounter?.images.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! EncounterCollectionViewCell
cell.imageView.sd_setImage(with: URL(string: (selectedEncounter?.images[indexPath.row])!))
return cell
}
Encounter Data structure
encounters
-12
-name: "shark"
-length: "3"
-media
-0
-id: "3242"
-url: "http://google.com"
-thumb-url: "http://thisurl.com"
-1
-id: "4252"
-url: "http://google.com"
-thumb-url: "http://thisurl.com"