首页 > 解决方案 > 无法使具有标识符 cellId 的单元出列 - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元

问题描述

这段代码曾经在 8.3 中工作,但由于我使用 Xcode 9.3 将应用程序移到了我的新计算机上,所以我收到了错误。

无法使具有标识符 cellId 的单元出列 - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元

尽管在我的代码中我清楚地注册了单元格。

import Foundation
import UIKit

class ServiceTypeSelector: UITableViewController,UINavigationBarDelegate {
  let defaults = UserDefaults.standard

  let sections = ["All Users & Services","Automotive", "Building & Construction", "Cleaning", "Landscaping & Gardening"]
  let services = [["All Users & Services"],["Automotive"],["Air Conditioning & Heating","Bricklaying", "Carpentry","Carpet Layer","Concreting & Paving","Electrical","Fencing and Gates","Flooring","Handyman","Other","Painting & Decorating","Pet Control","Plastering","Plumbing","Roofing","Rubbish Removal","Scaffolding","Tiling"], ["Cleaning"],["Landscaping & Gardening"]]

  //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()
    }

    return self.sections[section]

  }

  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 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?
  var sectionsSelector: ServiceTypeSelector?
  var serviceSelector: ServiceTypeSelector?

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  let refinementsLabel: UILabel = {
    let label = UILabel()
    label.text = "Sample Item"
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.boldSystemFont(ofSize: 14)
    return label
  }()
  let selectedRefinementsLabel: UILabel = {
    let b = UILabel()
    b.text = "Select"
    b.font = b.font.withSize(14)
    b.translatesAutoresizingMaskIntoConstraints = false
    return b
  }()

  func setupViews() {
    addSubview(refinementsLabel)
    addSubview(selectedRefinementsLabel)

    //selectedPreferenceLabel.addTarget(self, action: #selector(MyCell.handleAction), for: .touchUpInside)

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]-2-[v1(80)]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": refinementsLabel, "v1": selectedRefinementsLabel]))

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": refinementsLabel]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": selectedRefinementsLabel]))

  }
}

标签: swiftxcode

解决方案


扔掉guard let并替换所有这些:

guard let serviceBeingSearched = self.defaults.string(forKey: "Service being searched") else { return }
navigationItem.title = serviceBeingSearched
self.navigationController!.navigationBar.topItem!.title = ""

有了这个:

navigationItem.title = self.defaults.string(forKey: "Service being searched")

推荐阅读