我试图创建一个包含多个单元格的视图集合。
第一个单元格必须包含时间线。
如图所示,无法横向打印时间线。
如何水平打印此单元格?
我的代码:
class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var year = 2000
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 200
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 97, height: 33)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyViewCell
cell.textView.text = "\(year)";
year++;
return cell
}
}
答案 0 :(得分:2)
默认的集合视图布局应该已经像这样(来自Apple Docs):
您如何设置每个单元格的年份。不要使用year++
。让它回复indexPath.row
:
cell.textView.text = "\(2000 + indexPath.row)";
此处还有一个很棒的教程:UICollectionView Swift Tutorial