Petravd1994
Petravd1994

Reputation: 903

How to subclass a UITableViewCell?

I got this old post: How to subclass UITableView? and this old video: https://www.youtube.com/watch?v=FfZGKx4BYVU but this did not help me. What I want is very simple, I got an UITableViewController with dynamic cells. I want to outlet those cells. This is only possible if I "subclass" it. I made another file what looks like this:

import UIKit

class CreateChannelCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

But I still can not add those outlets from the UITableViewController to this file. My cells needs to be dynamic. I think I am only missing one step, but what step is that? Thank you.

Upvotes: 0

Views: 870

Answers (2)

anho
anho

Reputation: 1735

That is the right way to subclass. But if you are using Storyboards or Xib files then you need to reference the CreateChannelCell in the UITableView that you created and where the CreateChannelCell is located.

You need to select the cell and then go to CustomClass and write the name of your class there ... in this case CreateChannelCell

Here you need to go at your Cell and declare <code>CreateChannelCell</code>

If you are not using Storyboards or Xib's please see the answer provided by Code Different

Upvotes: 1

Code Different
Code Different

Reputation: 93191

You need to register your custom class with the table view.

If the cell is on the same scene as your table view:

tableView.register(MyCustomCell.self, forCellReuseIdentifier: "cell")

If you designed it on a different nib ( say MyCustomCell.xib):

let nib = UINib(nibName:@"MyCustomCell" bundle: .main)
tableView.register(nib, forCellReuseIdentifier: "cell")

Upvotes: 2

Related Questions