swift - 无法使具有标识符 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]))
}
}
解决方案
扔掉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")
推荐阅读
- c++11 - OpenCL 矩阵乘法运行,但答案始终为零
- java - Android/Java:即使应用程序没有“积极”使用,如何向用户发送通知?
- javascript - 将函数传递给语义 ui clearable prop
- javascript - 未定义将类转换为钩子“道具”
- node.js - 从帖子获取重定向不会导致查看更新
- html - Angular Flex 不能将项目放在同一行,Angular 8.2.1 和 Flex-Layout 8.0.0.beta 26
- javascript - 反应:无法通过将函数传递给更新它的子组件来更新父状态
- android - 如何修复屏幕锁定后Android MediaPlayer自动关闭
- python - 从python中的daraframe创建数据框列表
- c# - 通过 Wifi Lan 连接将数据从 Desktop Sqlite DB 检索到 android 应用程序