Swift 无法将标识符为cellId的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格
这段代码以前在8.3中工作,但自从我用Xcode 9.3将应用程序移动到我的新计算机上后,我就得到了错误 无法将标识符为cellId的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格 虽然在我的代码中我清楚地注册了手机Swift 无法将标识符为cellId的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格,swift,xcode,Swift,Xcode,这段代码以前在8.3中工作,但自从我用Xcode 9.3将应用程序移动到我的新计算机上后,我就得到了错误 无法将标识符为cellId的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格 虽然在我的代码中我清楚地注册了手机 import Foundation import UIKit class ServiceTypeSelector: UITableViewController,UINavigationBarDelegate { let defaults = UserDe
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]))
}
}
您必须按如下方式注册nib:
扔掉
防护罩,让它
更换所有这些:
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")
我把代码放在视图中,虽然我放了tableview.register(UINib(nibName:“ServiceCell”,bundle:nil),forCellReuseIdentifier:“cellId”),但确实加载了,现在我得到了一个错误:无法在bundle中加载NIB。。。还有“ServiceCell”这个名字,我想补充一点,我没有使用故事板。你使用xib?man这么长时间了,但我发现,我的guard let语句返回了零,所以它在viewDidLoad末尾注册cellUse guard语句之前就消失了
navigationItem.title = self.defaults.string(forKey: "Service being searched")