自从我将我的应用程序转移到我的新计算机后,我得到了一个错误:
无法将标识符为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/

10-10 19:46