Stephen501
Stephen501

Reputation: 161

Why doesn't my table view cell class instance run its initializer in my Swift code?

I have a custom subclass of UITableViewCell shown below

class GroupSelectionTableViewCell: UITableViewCell {

    // MARK: - Properties
    
    var groupSelectionLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.backgroundColor = .clear
        label.font = UIFont.systemFont(ofSize: 18)
        return label
    }()
    
    var groupSelectionImageView: UIImageView = {
        let iv = UIImageView()
        iv.backgroundColor = .clear
        iv.setHeight(height: 25)
        iv.setWidth(width: 25)
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    // MARK: - LifeCycle
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

         configureUI()
     }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Helper Functions
    private func configureUI() {
        
        self.addSubview(groupSelectionLabel)
        groupSelectionLabel.centerY(inView: self)
        groupSelectionLabel.anchor(left: self.leftAnchor, paddingLeft: 12)
        
        self.addSubview(groupSelectionImageView)
        groupSelectionImageView.centerY(inView: self)
        groupSelectionImageView.anchor(right: self.rightAnchor, paddingRight: 12)
        
        self.backgroundColor = .black
        self.selectionStyle = .none
    }
}

and a custom subclass of UITableView shown below...

class GroupSelectionView: UITableView {
    
    // MARK: - Properties
    
    private let cellID = "GroupSelectionTableViewCell"

    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        backgroundColor = .red
        setHeight(height: 450)
        setWidth(width: 300)
        register(GroupSelectionTableViewCell.self, forCellReuseIdentifier: cellID)
        rowHeight = 60
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension GroupSelectionView: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        return
    }
}

extension GroupSelectionView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! GroupSelectionTableViewCell
        cell.groupSelectionLabel.text = "None"
        cell.groupSelectionImageView.image = UIImage(named: "Tick")
        
        return cell
    }
}

But when I add my table view instance to a UIView() as a subview the table view cell class isn't running its initializer. Am I using the register method correctly? I have tried instantiating the table view subclass and putting a print statement in the initializer of the table view cell subclass but it doesn't get printed. Am I forgetting to set some property on the table view subclass? Do I need to use the Nib version of register instead? Any help would be greatly appreciated.

Upvotes: 0

Views: 108

Answers (1)

Stephen501
Stephen501

Reputation: 161

Forgot to set the dataSource and the delegate!

Upvotes: 1

Related Questions