dequeue reusable cell always crashes
Asked Answered
Z

7

9

in this code i add the programatic VC:

@IBAction func addPerson(_ sender: UIBarButtonItem) {
        let controller = CNContactPickerViewController()
        controller.delegate = self
        navigationController?.present(controller, animated: true, completion: nil)
    }

here i use to go on another VC when i click on a contact (to show info into cells and then i want to make a checkbox for phones/emails to show back in my first VC)

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        let vc = EditContactViewController()
        self.navigationController?.pushViewController(vc, animated: true)

i created a class for the next cell and i set up identifier as cell in storyboard when i do this in tableView cellForRowAtIndexPath

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? ConfigCell

return cell!

this gives me:

fatal error: unexpectedly found nil while unwrapping an Optional value

i also tried withIdentifier,forIndexPath and it says:

2016-11-12 01:35:28.626 iEvents3[2637:47714] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

i deleted reuse identifier and put it back again , cleaned the application, restart, everything.

Thank You for your time!

Zetland answered 11/11, 2016 at 23:38 Comment(0)
L
6

There can be several reason for this:

  1. If you have your UITableViewCell in controller itself, then check the reuse identifier whether it is set to "cell"

  2. If you have a separate .xib for your UITableViewCell, then

    a) Register the .nib of cell with your table view in viewDidLoad() of your controller

    self.tableView.register(UINib(nibName:"CustomTableViewCell"), forCellWithReuseIdentifier: "cell")
    

    Register the .nib only when you are sure the outlet of table view is set. Otherwise, if the outlet if table view is still nil, it won't register the .nib of your cell.

    b) Check whether the reuse identifier is set to "cell"

Lemming answered 12/11, 2016 at 16:3 Comment(1)
i solved this problem with : let v = self.storyboard?.instantiateViewController(withIdentifier: "help") self.navigationController?.pushViewController(v!, animated: true) in storyboard i put storyboard identifier for the v controller "help"Zetland
T
7

Make sure there is no '!' sign in interface outlet connections

enter image description here

Trueman answered 4/1, 2018 at 9:7 Comment(0)
L
6

There can be several reason for this:

  1. If you have your UITableViewCell in controller itself, then check the reuse identifier whether it is set to "cell"

  2. If you have a separate .xib for your UITableViewCell, then

    a) Register the .nib of cell with your table view in viewDidLoad() of your controller

    self.tableView.register(UINib(nibName:"CustomTableViewCell"), forCellWithReuseIdentifier: "cell")
    

    Register the .nib only when you are sure the outlet of table view is set. Otherwise, if the outlet if table view is still nil, it won't register the .nib of your cell.

    b) Check whether the reuse identifier is set to "cell"

Lemming answered 12/11, 2016 at 16:3 Comment(1)
i solved this problem with : let v = self.storyboard?.instantiateViewController(withIdentifier: "help") self.navigationController?.pushViewController(v!, animated: true) in storyboard i put storyboard identifier for the v controller "help"Zetland
V
3

Even if you don't have a xib for the cell, you may need to register it:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
Veldaveleda answered 16/5, 2018 at 9:23 Comment(0)
B
1

Do you have that cell as a xib? Did you add your cell to the table view? Try this if this is the case

@IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
self.tableView.register(UINib(nibName: "ConfigCell",bundle: nil), forCellReuseIdentifier: "cell")
}
Benitabenites answered 11/11, 2016 at 23:43 Comment(1)
did you do both in register in code and in storyboard?Frequently
D
1

In my case it was a silly mistake, just a misprint in the name of the .xib, but it took me too much time to find it because the name was very long and difficult to see the mistake, and so I was searching for other causes. It seems so obvious now.. but shit happens :)

I hope save your time if it's your case.

Driving answered 14/11, 2019 at 10:51 Comment(0)
Z
0

When cell expand it crash application

func tableView(_ tableView: SLExpandableTableView!, willExpandSection section: UInt, animated: Bool) {

}

func tableView(_ tableView: SLExpandableTableView!, didExpandSection section: UInt, animated: Bool)
{
    let indexPath = IndexPath(row: 0, section: Int(section))
    let cell: SLExpandableHeaderCell = (tableView.cellForRow(at: indexPath) as! SLExpandableHeaderCell)

    cell.empProfImage?.image = UIImage(named: "upArrowGrey.png")

    //        let imgCategorySymbol: UIImageView = (cell.viewWithTag(5030) as! UIImageView)
    //        imgCategorySymbol.image = UIImage(named: "upArrowGrey.png")

    for i in 0..<arrCategoriesServices.count
    {
        if !selectedSectionIndex.contains("\(i)") && i != Int(section) {

            tableView.collapseSection(i, animated: true)
        }
    }
}

func tableView(_ tableView: SLExpandableTableView!, willCollapseSection section: UInt, animated: Bool) {
    let indexPath = IndexPath(row: 0, section: Int(section))

    let cell: SLExpandableHeaderCell = (tblExpandableView.cellForRow(at: indexPath) as! SLExpandableHeaderCell)
    cell.empProfImage?.image = UIImage(named: "downArrowGrey.png")

    //        let imgCategorysymbol: UIImageView? = (cell.viewWithTag(5030) as! UIImageView)
    //        imgCategorysymbol?.image = UIImage(named: "downArrowGrey.png")
}

func tableView(_ tableView: SLExpandableTableView, canChangeSection section: Int) -> Bool {
    if collapsingSection != section {
        return true
    }
    else {
        return false
    }
}

func tableView(_ tableView: SLExpandableTableView!, didCollapseSection section: UInt, animated: Bool) {

}

func tableView(_ tableView: SLExpandableTableView, canExpandSection section: Int) -> Bool
{
    return true
}


func tableView(_ tableView: SLExpandableTableView!, needsToDownloadDataForExpandableSection section: Int) -> Bool
{
    return false
}

func tableView(_ tableView: SLExpandableTableView!, expandingCellForSection section: Int) -> UITableViewCell
{
    var cell: SLExpandableHeaderCell!

    if cell == nil
    {
        cell = tblExpandableView.dequeueReusableCell(withIdentifier: headerCell) as! SLExpandableHeaderCell
    }


    let label = UILabel()
    label.frame = CGRect(x: CGFloat(10), y: CGFloat(10), width: CGFloat(100), height: CGFloat(25))
    cell.addSubview(label)


    cell.empProfImage?.image = UIImage(named: "downArrowGrey.png")
    cell?.selectionStyle = .none

    let cat: CategoryModel = (arrCategory[section] as! CategoryModel)
    label.text = cat.categoryName
    print("Expanding table for categoty : \(cat.categoryName)")
  //  cell.textLabel?.frame = frame
  //  cell?.textLabel?.text = cat.categoryName

// Util.setCategoryDetailFontColorAndSize(label: (cell?.textLabel!)!, labelText: cat.categoryName) Util.setCategoryDetailFontColorAndSize(label: label, labelText: cat.categoryName) cell?.textLabel?.backgroundColor = UIColor.clear

    return cell!
}

public func tableView(_ tableView: SLExpandableTableView!, downloadDataForExpandableSection section: Int)
{

}
Zeidman answered 16/9, 2017 at 6:21 Comment(1)
Applicatio crash on this line let cell: SLExpandableHeaderCell = (tblExpandableView.cellForRow(at: indexPath) as! SLExpandableHeaderCell)Zeidman
I
0

For me, the TableView Cell was set to Static content instead of Dynamic since I want it to generate X numbers of cells based on my collection. A good clue was that I saw a weird Header Text with a blue cube above my Table View Cell in the hierarchy overview panel in Xcode.

Here's a visual where to set the Cells to Dynamic. xcode screenshot showing dynamic selection

Ixia answered 25/11, 2020 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.