Chaudhry Talha
Chaudhry Talha

Reputation: 7808

fatal error: unexpectedly found nil while unwrapping an Optional value in UITableViewCell

Similar question is asked here but that doesn't solve this problem for me. I've added a tableView in a ViewController. Made extended the class with it's dataSource and Delegate and added the required methods for it.
Then I made a prototype cell in this table (Not the separate .xib for it) and made a class for that TableViewCell and collected the @IBOutlet:

    @IBOutlet weak var titleOfAccount: UILabel!
    @IBOutlet weak var lastModified: UILabel!

    @IBOutlet weak var accountImage: UIImageView!
    @IBOutlet weak var cellView: UIView!

Then in cellForRowAt method of table view when I wrote

cell.titleOfAccount.text = "Hello World"
cell.lastModified.text = "Last Modified: 21 May 2017"

and ran the code it crashed with this error.

fatal error: unexpectedly found nil while unwrapping an Optional value

What I did after searching here and there I went back to tableViewCell class and changed ! to ? and updated the code in cellForRowAt as:

 cell.titleOfAccount?.text = "Hello World"
 cell.lastModified?.text = "Last Modified: 21 May 2017"

Now when I ran the program it runs successfully but there is no cell appearing in the tableView and I've also implemented the method:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3;
    }

Here is the full code of my cellForRowAt method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell: AccountsTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! AccountsTableViewCell

        cell.backgroundColor = UIColor.clear
        cell.clipsToBounds = true

        cell.cellView.layer.shadowColor = UIColor.black.cgColor
        cell.cellView.layer.shadowOpacity = 1
        cell.cellView.layer.shadowOffset = CGSize.zero
        cell.cellView.layer.shadowRadius = 10

        cell.titleOfAccount.text = "Hello World"
        cell.lastModified.text = "Last Modified: 21 May 2017"
        cell.accountImage.image = UIImage(named: "fb")

        return cell
    }

P.S. I've also added self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier) in viewDidLoad

enter image description here enter image description here

Upvotes: 15

Views: 19399

Answers (5)

Sajid Hasan
Sajid Hasan

Reputation: 21

I also face the same problem, and this works for me:

// old code
// self.tableView.register(YourTableViewCell.self, forCellReuseIdentifier: "YourCellReuseIdentifier")

// new code
self.tableView.register(UINib(nibName: YourTableViewCell, bundle: nil), forCellReuseIdentifier: "YourCellReuseIdentifier")

Upvotes: 0

Yoshi
Yoshi

Reputation: 91

I think you can resolve this by modify the code in the ViewController.

Use below

self.tableView.register(UINib(nibName: "AccountsTableViewCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)

instead of

self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

Upvotes: 6

Yogesh Rathore
Yogesh Rathore

Reputation: 183

Step 1 :- Create UITableView Extension Class

//MARK: - UItableView Extension
extension UITableViewCell {
// Not using static as it wont be possible to override to provide custom storyboardID then
class var storyboardID : String {
    return "\(self)"
  }

static func registerCellXib(with tableview: UITableView){
    let nib = UINib(nibName: self.storyboardID, bundle: nil)
    tableview.register(nib, forCellReuseIdentifier: self.storyboardID)
  }
}

Step 2 :- In ViewDidLoad method register cell

AudioTableViewCell.registerCellXib(with: audioListTableView)

here AudioTableViewCell is tableviewCell and audioListTableView is Tableview

Step 3 :- In CellForRowAt index method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: AudioTableViewCell.storyboardID, for: indexPath) as! AudioTableViewCell
    cell.audioFileName.text = "Audio \(indexPath.row + 1)"
    
    return cell
 }

Upvotes: 1

Krunal
Krunal

Reputation: 79646

From your viewDidLoad() remove following line (code), as you already have connected your Cell from story board.

self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

Also share snapshot for identity inspector (just left side of attribute inspector).

Upvotes: 23

Kais Tersim
Kais Tersim

Reputation: 21

Try this

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
          let cell = tableView.dequeueReusableCell(withIdentifier: "yourcellidentifier", for: indexPath) as! AccountsTableViewCell
          // add your code
    }

Upvotes: 0

Related Questions