0
0

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.

self sizing cell

Posted at

FlowLayout

flowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize

collectionViewCell

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var titleLabel: UILabel! {
        didSet {
            titleLabel.text = nil
        }
    }
    @IBOutlet weak var textLabel: UILabel! {
        didSet {
            textLabel.text = nil
        }
    }
    @IBOutlet weak var separator: UIView!
    @IBOutlet weak var widthLayout: NSLayoutConstraint?

    override func awakeFromNib() {
        super.awakeFromNib()
        // 本当は CollectionView の幅とかを指定したほうが良い
        setCellWidth(UIScreen.main.bounds.width)
    }

    func setCellWidth(_ width: CGFloat) {
        widthLayout?.constant = width
    }

    class func nib() -> UINib {
        return UINib(nibName: "\(self)", bundle: nil)
    }

    override func prepareForReuse() {
        super.prepareForReuse() // Don't forget!
    }
}
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?