LoginSignup
0
2

More than 5 years have passed since last update.

UICollectionViewCellを継承して実装した時の罠

Last updated at Posted at 2018-05-12

contentViewを使わないとダメらしい。

class MyCollectionViewCell: UICollectionViewCell {
    let image = UIImageView()

    override init(frame: CGRect) {
        super.init(frame: frame)

        picture.frame = contentView.frame
        self.contentView.addSubview(picture)
    }

ダメな例だと同じセルが沢山出たり、背景色だけで何も出なかったりして悲惨なことになります。
非常に分かりにくくてはまったorz

ストーリーボード経由でやってる人にはあんまし縁のない話かとは思います。

これはダメな例

class MyCollectionViewCell: UICollectionViewCell {
    let image = UIImageView()

    override init(frame: CGRect) {
        super.init(frame: frame)

        picture.frame = frame
        self.addSubview(picture)
    }
0
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
0
2