user9585671
user9585671

Reputation:

Unable to dequeue a cell with identifier cellID. Same code works fine on older Swift

Since I shifted my app to my new computer I'm getting the error:

Unable to dequeue a cell with identifier cellID

though the code is exactly the same as it was on my old computer and on my old computer it works fine.

Here is the code from the page giving the error:

import Foundation
import UIKit

class ServiceTypeSelector: UITableViewController,UINavigationBarDelegate {
    let defaults = UserDefaults.standard
    let sections = ["All Users & Services"]
    let services = [["All Users & Services"]]

    //active for business accounts only

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.backgroundColor = UIColor.gray

        guard let serviceBeingSearched = self.defaults.string(forKey: "Service being searched") else { return }

        navigationItem.title = serviceBeingSearched

        self.navigationController!.navigationBar.topItem!.title = ""

        tableView.register(ServiceCell.self, forCellReuseIdentifier: "cellId")
        tableView.register(Header.self, forHeaderFooterViewReuseIdentifier: "headerId")

        tableView.sectionHeaderHeight = 50
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let serviceBeingSearched = self.defaults.string(forKey: "Service being searched")

        if serviceBeingSearched == nil {
            defaults.set("All Users & Services", forKey: "Service being searched")

            tableView.reloadData()
        }
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

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

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell

        var service = serviceTypeCell.refinementsLabel.text
        service = services[indexPath.section][indexPath.row]

        defaults.set(service, forKey: "Service being searched")

        guard let serviceBeingSearched = self.defaults.string(forKey: "Service being searched") else { return }

        navigationItem.title = serviceBeingSearched

        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell
        serviceTypeCell.refinementsLabel.text = services[indexPath.section][indexPath.row]

        serviceTypeCell.sectionsSelector = self

        return serviceTypeCell
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId")
    }
}

class ServiceCell: UITableViewCell {
    var serviceTypeSelector: ServiceTypeSelector?

    //all other code removed for simplicity for the question
}

Upvotes: 0

Views: 83

Answers (2)

Gavin Tsang
Gavin Tsang

Reputation: 79

It's not right to call dequeReusableCell in didSelectRowat method. Use cellForRowAtIndexPath instead.

Upvotes: 0

matt
matt

Reputation: 535222

The problem is here:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let serviceTypeCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ServiceCell

You must not dequeue a cell in didSelectRowAt.

Upvotes: 1

Related Questions