我得到一个错误 - nib 必须恰好包含一个顶级对象,它必须是一个 UITableViewCell 实例'”
I am getting an error - nib must contain exactly one top level object which must be a UITableViewCell instance'"
我在我的 table 视图中使用了自定义单元格,但是当我 运行 我得到了我在问题中提到的错误。
self.districtTableView.register(UINib(nibName: "PlaceCollectionViewCell", bundle: nil), forCellReuseIdentifier: "placeCell")
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
// TODO: Your app can do something when textField finishes editing
print("The textField ended editing. Do something based on app requirements.")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return districts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell
// Set text from the data model
cell.distLabel.text = districts[indexPath.row]
cell.textLabel?.font = distTextField.font
return cell
我怎样才能摆脱这个错误。我使用了不同的方法在 table 视图中注册单元格。但它不起作用。请帮忙
如果你正在使用 tableviewcell 的 xib,那么在 viewDidLoad 中像这样注册你的 xib。
tableView.registerNib(UINib(nibName: "PlaceTableViewCell", bundle: nil), forCellReuseIdentifier: "placeCell")
如果您使用的是自定义 class 的 tableViewcell,那么试试这个,
let placeCell : PlaceTableViewCell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell
正如我所看到的,您的代码几乎是正确的。希望我的回答对你有帮助。
打开您的 'PlaceCollectionViewCell.xib' 文件。确保只有一个顶层视图(查看侧面板,而不仅仅是 canvas,它可能不可见)。确保你的视图有一个 class 分配,它是 UITableViewCell 的子class(不是 UICollectionViewCell,xib 名称对我来说看起来很可疑),以及 Reuse Identifier。
在 .xib 文件中有多个 table 视图单元格也会导致此错误。
将 table 视图单元移动到它们自己单独的 .xib 文件中解决了我的错误。
nib must contain exactly one top level object which must be a UITableViewCell instance
我已从界面生成器 (XIB) 添加了点击手势。
删除即可解决此问题。
您必须通过编程方式
在xib中添加gesture
该错误可能具有误导性。我以某种方式将一些视图拖到我的单元格中,它在主视图之外创建了另一个视图级别。只需将其从您的 xib 中删除即可,错误必须消失
我遇到了同样的问题,我的牢房中没有所有必要的限制。看看
在我的例子中,它是一个添加到单元格外部的视图。它不应该在那里
确保“属性检查器”面板中的“标识符”属性 设置为代码中指定的重用标识符。
我在我的 table 视图中使用了自定义单元格,但是当我 运行 我得到了我在问题中提到的错误。
self.districtTableView.register(UINib(nibName: "PlaceCollectionViewCell", bundle: nil), forCellReuseIdentifier: "placeCell")
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
// TODO: Your app can do something when textField finishes editing
print("The textField ended editing. Do something based on app requirements.")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return districts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell
// Set text from the data model
cell.distLabel.text = districts[indexPath.row]
cell.textLabel?.font = distTextField.font
return cell
我怎样才能摆脱这个错误。我使用了不同的方法在 table 视图中注册单元格。但它不起作用。请帮忙
如果你正在使用 tableviewcell 的 xib,那么在 viewDidLoad 中像这样注册你的 xib。
tableView.registerNib(UINib(nibName: "PlaceTableViewCell", bundle: nil), forCellReuseIdentifier: "placeCell")
如果您使用的是自定义 class 的 tableViewcell,那么试试这个,
let placeCell : PlaceTableViewCell = tableView.dequeueReusableCell(withIdentifier: "placeCell") as! PlaceTableViewCell
正如我所看到的,您的代码几乎是正确的。希望我的回答对你有帮助。
打开您的 'PlaceCollectionViewCell.xib' 文件。确保只有一个顶层视图(查看侧面板,而不仅仅是 canvas,它可能不可见)。确保你的视图有一个 class 分配,它是 UITableViewCell 的子class(不是 UICollectionViewCell,xib 名称对我来说看起来很可疑),以及 Reuse Identifier。
在 .xib 文件中有多个 table 视图单元格也会导致此错误。 将 table 视图单元移动到它们自己单独的 .xib 文件中解决了我的错误。
nib must contain exactly one top level object which must be a UITableViewCell instance
我已从界面生成器 (XIB) 添加了点击手势。
删除即可解决此问题。
您必须通过编程方式
在xib中添加gesture该错误可能具有误导性。我以某种方式将一些视图拖到我的单元格中,它在主视图之外创建了另一个视图级别。只需将其从您的 xib 中删除即可,错误必须消失
我遇到了同样的问题,我的牢房中没有所有必要的限制。看看
在我的例子中,它是一个添加到单元格外部的视图。它不应该在那里
确保“属性检查器”面板中的“标识符”属性 设置为代码中指定的重用标识符。