2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Playground で UICollectionView の Cell の調整をする

Last updated at Posted at 2015-03-03

UICollectionViewCell をカスタマイズする

Playground で UITableView の Cell の調整をする の UICollectionView バージョンです。

CustomCollectionViewCell の内容を書き換えて完成したらプロジェクトのカスタムセルにコピペします。

Playground って便利ですね。

import UIKit
import XCPlayground

class CustomCollectionViewCell: UICollectionViewCell{
    
    override init(frame: CGRect){
        super.init(frame: frame)

        // ここを書き換えることで UI の調整をする
        var label = UILabel(frame: CGRectMake(0, 0, 30, 30))
        label.text = "test"
        label.backgroundColor = UIColor.whiteColor()
        self.contentView.addSubview(label)
    }
    
    required init(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }
}

class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var collectionView: UICollectionView!
    let items = ["Item 1", "Item 2", "Item 3"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Vertical
        flowLayout.minimumInteritemSpacing = 5.0
        flowLayout.minimumLineSpacing = 5.0
        flowLayout.itemSize = CGSizeMake(40, 40)
        
        self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: flowLayout)
        self.collectionView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(self.collectionView)
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.redColor()
        return cell
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 42
    }
}

var ctrl2 = CollectionViewController()
XCPShowView("Playground collection view", ctrl2.view)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?