Help us understand the problem. What is going on with this article?

UICollectionViewのDataSourceとDelegateをControllerと別のファイルに記述する

More than 5 years have passed since last update.

経緯

UICollectionViewを1つのControllerの中で2つ使いたい機会があって調べ始めた。
タグを設定するという方法も出来そうであったがゴチャゴチャしそうだったのでファイルを分ける方向にした。
1つの時でもDataSourceとDelegateはそこそこ長いので、別ファイルにすると、Controllerがスッキリする!
TableViewやObjective-Cでのサンプルはいくつかみつけたが、
CollectionViewのSwiftでのサンプルが少なかったので単純なサンプルを残しておく・x・っ

ファイル

ViewController.swift:コントローラー
↓呼び出し
UsagiConfig.swift:DataSourceとDelegate
↓呼び出し
CustomUsagiCell.swift:セルの定義

補足

この呼び出し方を複数にすれば、1つのControllerの中で複数のUICollectionViewを使うことができる。
DataSourceとDelegateを入れておくファイル名に悩んだ!
なにかもっとしっくりくる名前はないだろうか・x・・・
CollectionViewの設定はStoryBoardでしてます。

コード

ViewController.swift
import UIKit

class ViewController: UIViewController {

    var usagiConfig:UsagiConfig?

    @IBOutlet weak var usagiCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // create Usagi
        var items:Array<String> = []
        for ( var i = 1, n = 20 ; i <= n ; i++ ) {
            items.append("Usagi"+i.description)
        }

        // set Usagi List
        self.usagiConfig = UsagiConfig(items: items)
        self.usagiCollectionView.dataSource = self.usagiConfig
        self.usagiCollectionView.delegate = self.usagiConfig
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
UsagiConfig.swift
import UIKit

class UsagiConfig: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {

    var items:Array<String> = []

    init(items: Array<String>) {
        self.items = items
        super.init()
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell:CustomUsagiCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomUsagiCell

        // set Name
        cell.name.text = self.items[indexPath.row];

        return cell
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count;
    }
}
CustomUsagiCell.swift
import UIKit

class CustomUsagiCell: UICollectionViewCell {
    @IBOutlet var name:UILabel!
}

出力結果

usagicell.png

asami_usa
Perl(1997-2005)/PHP(2005-)/Objective-C(2011-2014)/Swift(2015/4-)/Ruby,Python,Javascript
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした