Unable to use dequeueReusableCell(withIdentifier: for:) from a xib file
Asked Answered
N

2

5

I am having trouble casting my table view cell to my custom table view cell in the dequeueReusableCell(withIdentifier: for:) step.

My code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
    cell.labelTitle.text = array[indexPath.row]
    return cell
}

I have registered my cell in viewDidLoad():

tableView.register(TextFieldTableViewCell.self, forCellReuseIdentifier: "TextFieldcell")

And I got an error saying:

fatal error: unexpectedly found nil while unwrapping an Optional value

I bet it failed the casting and that is why the cell is empty. Can anybody see the error?

Thanks!

UPDATED CODE

 override func viewDidLoad() {
        super.viewDidLoad()
        let bundle = Bundle(for: TextFieldTableViewCell.self)
        let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
        tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
        cell.labelTitle.text = array[indexPath.row]
        return cell
    }
}

So apparently the issue still persists, to clarify, here are the steps I took to create this custom UITableViewCell

  1. Create a .swift file and a .xib file
  2. inside of the .xib file, I have deleted the boilerplate code and added in two UILabels
  3. I set the file owner of the .xib file to my swift, which has my two IBOutlets
  4. Next, inside of my tableViewController where I want to instantiate my custom tableViewCell, inside the viewDidLoad() method, I added in the following:

let bundle = Bundle(for: TextFieldTableViewCell.self)

let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)

tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")

My reasoning for creating this code are as follow: since I have create my custom table view cell using a .xib file and interface builder, I need to load the xib into my tableViewController (correct me if I am wrong) using tableView.register(nibName: forCellReuseIdentifier:) function.

However, when I run the code, I got an error saying: reason: 'Could not load NIB in bundle: 'NSBundle

Here is my code for my custom cell:

class TextFieldTableViewCell: UITableViewCell {

    @IBOutlet weak var labelTitle: UILabel!
    @IBOutlet weak var userInput: UITextField!
}

Can anybody see where the problem is? thanks!

Northernmost answered 13/8, 2017 at 23:29 Comment(0)
W
7

You need to register your nib object.

In viewDidLoad():

let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)

tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")
Walcoff answered 14/8, 2017 at 5:18 Comment(0)
T
3

If your UITableViewCell subclass is designed in Interface Builder you should use the

func register(_ nib: UINib?, forCellReuseIdentifier identifier: String)

method. For example:

let nib = UINib(nibName: "\(TextFieldTableViewCell.self)", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")

If your UITableViewCell subclass is created entirely in code you should use the

func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String) 

method.

If you're using prototype cells in a storyboard you should not register your cells at all.

Trammel answered 13/8, 2017 at 23:56 Comment(6)
thanks for the reply, I have created my cell in a xib file and I have registered my cell, but somehow it failsNorthernmost
In your question you say you are using func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String) . But if you used an xib you should be using func register(_ nib: UINib?, forCellReuseIdentifier identifier: String). If that's not the case, perhaps you can update your question to more accurately reflect what you're doing.Trammel
Thanks that fixed it!!appreciate your help!Northernmost
sorry, actually the error still persists, I have updated my code above, can you please check? thanks! many help would be greatly appreciated! thanks!Northernmost
I've updated my answer. I should have been more specific. You should say something like let nib = UINib(nibName: "\(TextFieldTableViewCell.self)", bundle: nil); tableView.register(nib, forCellReuseIdentifier: "TextFieldcell"). I was using MyTableViewCell as an example of a UITableViewCell subclass name. Typically when you create an xib in Xcode it will have the same name as the class you are creating in your case TextFieldTableViewCell. We can pass nil in for the bundle because we want to use the default main bundle of the app.Trammel
Thanks for you input!Northernmost

© 2022 - 2024 — McMap. All rights reserved.