无法使用来自xib文件的dequeueReusableCell(withIdentifier:for :)
我在将表格视图单元格转换为dequeueReusableCell(withIdentifier: for:)
步骤中的自定义表格视图单元格时遇到问题。无法使用来自xib文件的dequeueReusableCell(withIdentifier:for :)
我的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
cell.labelTitle.text = array[indexPath.row]
return cell
}
我已经注册了viewDidLoad()
我的手机:
tableView.register(TextFieldTableViewCell.self, forCellReuseIdentifier: "TextFieldcell")
而且我得到了一个错误说:
fatal error: unexpectedly found nil while unwrapping an Optional value
我打赌失败铸造,这就是为什么细胞是空的。任何人都可以看到错误?
谢谢!
更新的代码
override func viewDidLoad() {
super.viewDidLoad()
let bundle = Bundle(for: TextFieldTableViewCell.self)
let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldcell", for: indexPath) as! TextFieldTableViewCell
cell.labelTitle.text = array[indexPath.row]
return cell
}
}
因此很明显,问题仍然存在,澄清,这里是我创建这个自定义的UITableViewCell
- 创建.swift文件和步骤。 xib文件
- 在.xib文件中,我删除了样板代码并添加了两个UILabels
- 我将.xib文件的文件所有者设置为我的swift,其中包含我的两个IBOutlets
- 接下来,在我的tableViewController中,我想实例化我自定义的tableViewCell,在
viewDidLoad()
方法中,我添加了以下内容:
let bundle = Bundle(for: TextFieldTableViewCell.self)
let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")
我创建此代码推理情况如下:■因为我使用.xib文件和接口生成器创建了我的自定义表格视图单元格,我需要使用tableView.register(nibName: forCellReuseIdentifier:)
函数将xib加载到我的tableViewController(纠正我,如果我错了)。
然而,当我运行代码,我得到了一个错误说:reason: 'Could not load NIB in bundle: 'NSBundle
这里是我的我的自定义单元代码:
class TextFieldTableViewCell: UITableViewCell {
@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var userInput: UITextField!
}
任何人都能看出问题出在哪里?谢谢!
如果您UITableViewCell
子类是设计在Interface Builder中你应该使用
func register(_ nib: UINib?, forCellReuseIdentifier identifier: String)
方法。例如:
let nib = UINib(nibName: "\(TextFieldTableViewCell.self)", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "TextFieldcell")
如果在代码中完全创建了UITableViewCell
子类,你应该使用
func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String)
方法。
如果您在故事板中使用原型单元格,则根本不应注册单元格。
您需要注册您的笔尖对象。
在viewDidLoad中():
let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")
感谢您的答复,我已经创建了我的手机在厦门国际银行文件,我已经注册了我的手机,但不知何故,失败 –
在你的问题你说你正在使用' func寄存器(_ cellClass:Swift.AnyClass ?, forCellReuseIdentifier标识符:字符串)'。但是如果你使用了一个xib,你应该使用'func register(_nib:UINib?,forCellReuseIdentifier identifier:String)'。如果情况并非如此,也许你可以更新你的问题,以更准确地反映你正在做的事情。 – beyowulf
谢谢修复它!感谢您的帮助! –