UICollectionView水平单元格
我试图创build一个视图集合与多个单元格。
第一个单元格必须包含一个时间线。
正如你在图片中看到的,不能横向打印时间线。
我怎样才能水平打印这个单元格?
我的代码:
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 } }
默认的集合视图布局应该已经像这样工作了(来自Apple Docs):
这是你如何设定每个细胞的年份。 不要使用year++
。 让它响应indexPath.row
:
cell.textView.text = "\(2000 + indexPath.row)";
这里还有一个很好的教程: UICollectionView Swift教程