我需要使用从[[2000到2050的年份列表来填充UICollectionView
的单元格。
示例2000年的12个单元格,2001年的另外12个单元格,等等...
快速5最好的方法是什么?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (2050-1999)*12
}
然后定义开始年份变量和月份名称数组
var year:Int = 1999 let months:[String] = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
现在您可以将数据添加到collectionview单元格
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //define cell if indexPath.row % 12 == 0 { year += 1 } cell.lblYear.text = "\(year)" cell.lblMonth.text = months[indexPath.row % 12] return cell }