Override Init for Custom Table Cell vs Custom Collection Cell

Collection view cells and Table view cells: similar, yet so different. One of the things I kept getting confused on was which override init to use for each one.

Collection View Custom Cell

class CustomCollectionCell: UICollectionViewCell {

    //Add Custom Views

    //Initialize
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //Add Subviews
        //Setup constraints
    }
}

Table View Custom Cell

class CustomTableCell: UITableViewCell {

    //Add Custom Views

    //Initialize
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //Add Subviews
        //Setup constraints
    }
}

And then of course regardless of either one, you need to include the NSCoder required init, otherwise Xcode will warn you of your impending doom.

required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

It’s not a super difficult amount to remember so I just need to make sure I don’t mix up the two.

Leave a Comment

Your email address will not be published. Required fields are marked *