LoginSignup
1
2

More than 1 year has passed since last update.

Collection ViewのCustom Cellの作り方

Last updated at Posted at 2022-11-08

どんなものができる?

Collection ViewのCustom Cellを作ることができるようになれば、Spotifyの上の方のこういう部品を作れるようになります!
Spotify例
この記事ではこれの再現を通してCustom Collection Viewの作り方を解説します!

1. StoryboardにColleciton Viewを配置する

Collection ViewをStoryboard上に配置し、
1-1
制約をつけます
今回は上と左右にSafeAreaに対する制約をつけて、高さを設定しました
1-2
今回はCustom Collection View Cellを自分で作るので、元々おいてあるCollection ViewCellを選択し、削除しましょう
image.png
こんな感じになっていればOK
image.png

2. Custom Cell用のファイルを作成

メニューバーの File > New > File...を押して、
image.png
Cocoa Touch Classを選択して「Next」
image.png
Subclass of: を、「UICollectionViewCell」にして、
Class: にわかりやすい名前を入力します
今回はCustomCellという名前に設定しました
Also create XIB fileにチェックを付けて、「Next」
image.png
すると、CustomCell.swiftと、CustomCell.xibというファイルができるはずです
image.png

3. Custom Cellの見た目を設定する

CustomCell.xibを開きます
デフォルトだとCellの大きさが小さすぎて開発しづらいので、
image.png
テキトーに広げます
image.png
SpotifyのCellを観察してみると、
左側にアスペクト比1:1の画像、
右側に改行可能なラベル
が配置されてることがわかります
(ミセスまじで好き、、この記事もミセス聴きながら書いてました笑)
image.png
Image Viewを
image.png
Cellの上に配置し、
上下、左に距離の制約をつけ、
アスペクト比の制約をつけます
image.png
アスペクト比を1:1に設定し、
image.png
これでImage Viewの配置は完了です

同じようにLabelも配置し
image.png
上下左右に制約をつけ
image.png
改行可能にするためにLinesを0に設定します
image.png

これでCustomViewの見た目の設定が完了しました
image.png

4. Custom Cell Classを実装する

import UIKit

class CustomCell: UICollectionViewCell {
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var imageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        label.text = ""
        imageView.image = UIImage()
    }
    
    func setupCell(imageName: String, labelText: String) {
        label.text = labelText
        imageView.image = UIImage(named: imageName)
    }
    
}

やってること:

  • @IBOutletでUI部品の宣言をしています

  • awakeFromNib()は、xibファイルが初めて読み込まれるときに1度だけ呼ばれるメソッドです
    ViewControllerでいうところのviewDidLoadが近いと思います
    今回は特に何も書いていません

  • prepareForReuse()は、xibが再利用されるときに呼ばれるメソッドです
    再利用については、こちらの記事がわかりやすいので読んでみてください。
    主に初期化を行います

  • 最後に、setupCell()はCellをセットアップするためのメソッドです

ここまで書けたら、CustomCell.xibを開いて、関連づけをしましょう
image.png

5. ViewControllerを実装する

最後に、CustomCellを呼び出す側の実装です

ViewController.swift
import UIKit

class ViewController: UIViewController, UICollectionViewDataSource {
    @IBOutlet var collectionView: UICollectionView!
    
    let playlists = [
        ["green_apple", "青りんごさん"],
        ["karaoke_kids", "子供に人気"],
        ["karaoke_otoshiyori", "お年寄りに人気"],
        ["musician", "今人気のミュージシャン"],
        ["official_hige", "公式髭男爵"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCell")
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: collectionView.frame.width / 2 - 20 , height: 46)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        collectionView.collectionViewLayout = layout
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        playlists.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath)
        
        if let cell = cell as? CustomCell {
            cell.setupCell(imageName: playlists[indexPath.row][0], labelText: playlists[indexPath.row][1])
        }
        
        return cell
    }

}

主要な部分をそれぞれ解説していきます


まずViewDidLoad内のこれ

collectionView.dataSource = self

Collection ViewにはdataSourceというものがあって、こいつは、Collection View内のデータを管理するProtocolです。
Collection ViewをこのViewControllerで使うということを設定するために、dataSourceをselfに設定しています。


次はこの部分

collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCell")

Collection Viewで使用するセルをregister(_:forCellWithReuseIdentifier:)メソッドで登録しています。
第一引数にはUINibオブジェクトを渡します。
UINibのイニシャライザに渡しているCustomCellという文字列は、セルのXIBファイル名と一致させます。
第二引数のforCellWithReuseIdentifierには、セルの再利用識別子というものを指定します。
ここの文字列は任意ですが、ここではCustomCellを指定します。


let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: collectionView.frame.width / 2 - 20 , height: 46)
layout.sectionInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
collectionView.collectionViewLayout = layout

ここではCollection View Cellの大きさや隙間の大きさなどを設定しています。


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    playlists.count
}

このメソッドには、Collection View Cellを何個表示するのかを返します。


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath)
        
    if let cell = cell as? CustomCell {
        cell.setupCell(imageName: playlists[indexPath.row][0], labelText: playlists[indexPath.row][1])
    }
        
    return cell
}

この部分では、dequeueReusableCellでCustom Cellをとってきて、
Custom CellのsetupCellメソッドを呼び、cellを設定しています。


コードを書けたら、StoryBoardに移動し、collectionViewを関連付けしましょう。

6. 完成

色や大きさを微調整すれば
image.png
こんな感じにできましたー!

今回のソースコードは↓ここにあります

参考

1
2
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
1
2