UITableView - registerClass with Swift

Is anyone else having an issue using the tableView.registerClass method with Swift?

It no longer comes in code completion for me (nor can I use it if manually typed) but it is still in the headers...

Code Completion

UITableView headers


Solution 1:

It works for me perfectly.

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

Exactly as I have it above.

Solution 2:

For Swift 2.2 Register For Default Cell From Class

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")



For Swift 3.0 Register For Default Cell From Class

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")



For Swift 2.2 Register For Default Cell From Nib

self.tableView.registerNib(UINib(nibName: "CustomCellName", bundle: nil), forCellReuseIdentifier: "CustomCellIdentifer")



For Swift 3.0 Register For Default Cell From Nib

self.tableView.registerNib(UINib(nibName: "CustomCellName", bundle: nil), forCellReuseIdentifier: "CustomCellName")

Note: Storyboard created cell is called prototype cell and have some procedure to register prototype cell like Nib.And Don't forget to set the cell identifier like below. enter image description here

Solution 3:

Swift has once again renamed it to

tableView.register(UITableViewCell.self, forCellReuseIdentifier:"DefaultCell")

Really don't understand why they bothered so much about this particular naming

Solution 4:

Updated for Swift 5

Register TableView Cell in viewDidLoad If you are using Default Cell

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyTableCell")

Register TableView Cell in viewDidLoad If you are using Custom Nib/XIB Cell

tableView.register(UINib(nibName: "MyCustomCell", bundle: nil), forCellReuseIdentifier: "MyCustomCell")

Solution 5:

I prefer to use TableViewCell.self to generate the identifier string. It can reduce the typing error.

tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))