I need to populate the cells of a UICollectionView
with a list of years from 2000 to 2050.
Each cell must contain one month of the year up to the twelfth month and then start again with a new year.
Example 12 cells for the year 2000, another 12 cells for the year 2001 etc ...
What is the best way to go with swift 5?
you can set cell count as (2050-2000)*12
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (2050-2000)*12
}
then define start year variable and months name array
var year:Int = 2000
let months:[String] = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
now you can add data to collectionview cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//define cell
cell.lblYear.text = "\(year+indexPath.row.quotientAndRemainder(dividingBy: 12).quotient)"
cell.lblMonth.text = months[indexPath.row % 12]
return cell
}