UITableView - registerClass with Swift

IosSwiftUitableviewRegisterclass

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

It works for me perfectly.

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

Exactly as I have it above.

Solution 2 - Ios

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 - Ios

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 - Ios

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 - Ios

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))

Solution 6 - Ios

For swift 4

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

Solution 7 - Ios

Swift 4 and 4.1. making generic methods it is very easy to register and dequeue table cell.

       override func viewDidLoad() {
                super.viewDidLoad()
                self.tblView.register(CellProfileOther.self) // cell class name
       }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell: CellProfileOther = tableView.dequeueReusableCell(forIndexPath: indexPath)
                return cell
            }
        extension UITableView {
        func register<T:UITableViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {
                let bundle = Bundle(for: T.self)
                let nib = UINib(nibName: T.nibName, bundle: bundle)
                self.register(nib, forCellReuseIdentifier: T.defaultReuseIdentifier)
            }
        func dequeueReusableCell<T:UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
                guard let cell = self.dequeueReusableCell(withIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
                    fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)")
                }
                return cell
            }
        }
        
        protocol ReusableView: class {
            static var defaultReuseIdentifier: String { get }
        }
        
        protocol NibLoadableView: class {
            static var nibName: String { get }
        }
        
        extension ReusableView where Self: UIView {
            static var defaultReuseIdentifier: String {
                return String(describing: Self.self)
            }
        }    
        extension NibLoadableView where Self: UIView {
            static var nibName: String {
                return String(describing: Self.self)
            }
        }
    
   // Here is cell class 
    class CellProfileOther: UITableViewCell, ReusableView, NibLoadableView  {
    }

Solution 8 - Ios

For swift 3 refer this. It works!

Inside you viewdidload function

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

Solution 9 - Ios

Switching the order in which I called registerNib and registerClass worked for me!

For some reason my app crashed when I had:

...registerNib.....

...registerClass...

But ran fine when I had:

...registerClass...

...registerNib.....

Hope that helps some of you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStuart BreckenridgeView Question on Stackoverflow
Solution 1 - IosWoodstockView Answer on Stackoverflow
Solution 2 - IosDinesh KatwalView Answer on Stackoverflow
Solution 3 - IosQiulangView Answer on Stackoverflow
Solution 4 - IosswiftBoyView Answer on Stackoverflow
Solution 5 - IosZgpeaceView Answer on Stackoverflow
Solution 6 - IosRinju JainView Answer on Stackoverflow
Solution 7 - IosGurjinder SinghView Answer on Stackoverflow
Solution 8 - IosAmit ThakurView Answer on Stackoverflow
Solution 9 - IosHenry HeleineView Answer on Stackoverflow