UICollectionView水平单元格

时间:2014-12-05 11:08:38

标签: ios swift uicollectionview

我试图创建一个包含多个单元格的视图集合。

第一个单元格必须包含时间线。

如图所示,无法横向打印时间线。

如何水平打印此单元格?

我的代码:

      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
    }
}

1 个答案:

答案 0 :(得分:2)

默认的集合视图布局应该已经像这样(来自Apple Docs):

collection view layout

您如何设置每个单元格的年份。不要使用year++。让它回复indexPath.row

cell.textView.text = "\(2000 + indexPath.row)";

此处还有一个很棒的教程:UICollectionView Swift Tutorial