ios - 无法使具有标识符 cellID 的单元格出队。相同的代码在旧的 Swift 上运行良好

标签 ios swift uitableview

自从我将我的应用程序转移到我的新计算机后,我收到了错误:

Unable to dequeue a cell with identifier cellID

尽管代码与我的旧计算机上的代码完全相同,但在我的旧计算机上它运行良好。

这是错误页面中的代码:

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
}

最佳答案

问题出在这里:

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

必须不使 didSelectRowAt 中的单元格出队。

关于ios - 无法使具有标识符 cellID 的单元格出队。相同的代码在旧的 Swift 上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054787/

相关文章:

swift - 从 NSNotificationCenter 执行 reloadData() 后 UITableView 为空

ios - Swift - 通过引用获取约束或遍历属性的约束?

iphone - 如何在 iOS 应用程序中实现自定义(平面图图像) map ……?

ios - 如何在单个文件中定义的多个 View Controller 中使用属性?

swift - 无法在 Firestore 中创建新文档

swift - Collection View 单元格自动布局每个单元格的不同高度

swift - 在 Swift 中重复 CGAffineTransformMakeRotation

iphone - 是否有可能在 iOS 上获得列出的注册电子邮件帐户?

iphone - 自定义表格 View 单元格中没有可见 View

ios - UITableView 在单元格刷新时滚动到顶部