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

13,321

Solution 1

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

Solution 2

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)

Solution 3

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
 }
Share:
13,321
Chaudhry Talha
Author by

Chaudhry Talha

Updated on June 09, 2022

Comments

  • Chaudhry Talha
    Chaudhry Talha almost 2 years

    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

  • Chaudhry Talha
    Chaudhry Talha about 7 years
    No still getting the same: fatal error: unexpectedly found nil while unwrapping an Optional value
  • Kais Tersim
    Kais Tersim about 7 years
    check you cell identifier with storyboard please it should be the same of "yourcellidentifier"
  • Chaudhry Talha
    Chaudhry Talha about 7 years
    just checked it's the same at both ends accountCell
  • Chaudhry Talha
    Chaudhry Talha about 7 years
    Yes! I totally forgot this... #SwiftBeginners
  • Amit Khetan
    Amit Khetan over 3 years
    coming from swfit 5, had the same issue. deleting the registration while already registered in interface builder, helped. Thanks. #SwiftBeginners