用年和月填充UICollectionViewCell

问题描述 投票:0回答:1

我需要使用从[[2000到2050的年份列表来填充UICollectionView的单元格。

每个单元格必须包含一年中的一个月,直到第十二个月,然后从新的一年开始。

示例2000年的12个单元格,2001年的另外12个单元格,等等...

enter image description here

快速5最好的方法是什么?

ios swift uicollectionview uicollectionviewcell
1个回答
1
投票
您可以将单元格计数设置为(2050-1999)* 12

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 }

© www.soinside.com 2019 - 2024. All rights reserved.