Reputation: 1454
I have a collection view embedded in a table view so it scrolls vertically and horizontally. It has four sections, Today, This Week, This Month, This Year. I am having trouble understanding how to populate each section with its own data. Right now I am using the below code to populate the collection view but all sections have the same data. How can I populate each section with with its own data?
So if I had an array of images for Today, an array of images for This Week, an array of images for This Month, and and array of images for This Year, how can I set the image view in its section to the items in the corresponding array? So Today should have the images in the todayImageArray, This Week should have the images in the thisWeekImageArray and so on.
Also I have the delegate and data source of the collection view set to the table view.
Any help is much appreciated!
let images: [[UIImage]] = [
[image_1, image_2, image_3, image_4],
[image_5, image_6, image_7, image_8],
[image_9, image_10, image_11, image_12],
[image_13, image_14, image_15, image_16]
]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.section][indexPath.row]
return cell
}
Upvotes: 2
Views: 2583
Reputation: 3368
You need a structured data source. Currently, from your code, it looks like you have a single array called images
and you're using that to both provide the number of items and also to update cell content. But if you need separate sections, you need to model your datasource as such and provide logical responses to datasource and delegate methods in collectionView.
Example simple data source:
let simpleDataSource: [[UIImage]] = [
[image1, image2],
[image3, image4],
[image5, image6]
]
Example of using this in collectionView methods:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return simpleDataSource.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return simpleDataSource[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = simpleDataSource[indexPath.section][indexPath.row]
return cell
}
You should also read through the documentation for UICollectionView.
Upvotes: 6
Reputation: 11928
Set up an array that has all your images, or better yet structs designed to organize your information better, or image names as in my example, and then use the numberOfSections
and indexPath.section
properties to assign those images:
let images = [
["imageA1", "imageA2"],
["imageB1", "imageB2"]
]
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return images[section]
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images[section].count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = UIImage(named: images[indexPath.section][indexPath.row])
return cell
}
Upvotes: 0