I am brand new to this so please go easy on me!
My problem is as follows:
I am trying to populate a scroll view (UICollectionView) with data from firebase.
I am positive that it is retrieving the data successfully as I can print the data using a for loop at the end of the firebase function.
The problem is that when I try to insert the data into the scroll view it says the data is out of range!!
It has been pointed out to me that a scrollview is populated with the scrollView delegate methods which I have displayed below.
It was also pointed out to me that my data source is 'self.pages'. How to I pass the data retrieved from firebase to the delegate methods?
Fatal error: Index out of range
Here is my code:
//arrays of names and descriptions which are populated in firebase() function
var names:[String] = []
var descriptions: [String] = []
The page class:
let imageName: UIImage
let headerText: String
let bodyText: String
Here is the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
firebase()
setupImages()
}
Here is the image set up function:
func setupImages(){
self.pages = [
Page(imageName: self.image, headerText: names[0], bodyText: descriptions[0]),
Page(imageName: self.image, headerText: names[1], bodyText: descriptions[1]),
Page(imageName: self.image, headerText: names[2], bodyText: descriptions[2]),
Page(imageName: self.image, headerText: names[3], bodyText: descriptions[3]),
Page(imageName: self.image, headerText: names[4], bodyText: descriptions[4]),
]
self.collectionView?.backgroundColor = .white
self.collectionView?.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
self.collectionView?.isPagingEnabled = true
}
I think the issue is caused by the firebase data not being retrieved quick enough.
I say this because I have another function in a different class that seems to set it up:
override init(frame: CGRect) {
super.init(frame: frame)
//setup layout sets up constraints for the layout of the page
setupLayout()
}
Adding the UICollectionViewDataSource method's:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
and:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell
let page = pages[indexPath.item]
cell.page = page
return cell
}
Firebase retrieval method:
func firebase()
{
//connection to firebase for the names and descriptions
let db = Firestore.firestore()
db.collection(restaurauntName).getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in snapshot!.documents {
let name = document.get("Name") as! String
let description = document.get("Description") as! String
//Add names and descriptions to the arrays
self.names.append(name)
self.descriptions.append(description)
}
}
for x in self.names{
print(x)
}
}
}
Thank you for reading, any way someone can put me in the right direction would be appreciated!