使用collection view controller快速建立格子狀版面

  1. 在storyboard加入UICollectionViewController物件。

  2. 設定collection view cell的Reuse identifier為Cell。

  3. 將collection view cell的背景顏色設為黃色。

  1. 設定cell的size。(Cell Size的Width和Height欄位)

  1. 新增繼承UICollectionViewController的MyCollectionViewController。

  2. 將剛剛storyboard裡加入的collection view controller類別設為MyCollectionViewController。

  3. 修改MyCollectionViewController.swift

  4. 將viewDidLoad裡的registerClass注解

//self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  • 設定cell
 override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 10
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

        // Configure the cell

        return cell
    }

結果

results matching ""

    No results matching ""