ios swift制作表格列表不断获取关于nill的错误
上面是我看到要遵循的代码 我的代码如下 我不知道为什么它没有价值 tableView.dequeueReusableCell(标识符为“locCell”) 我的故事板如下 我添加了如下所示的标识符(你们可以在图片的右下角看到它)ios swift制作表格列表不断获取关于nill的错误,ios,swift,xcode,tableview,Ios,Swift,Xcode,Tableview,上面是我看到要遵循的代码 我的代码如下 我不知道为什么它没有价值 tableView.dequeueReusableCell(标识符为“locCell”) 我的故事板如下 我添加了如下所示的标识符(你们可以在图片的右下角看到它) 您需要注册该单元以便重新使用 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = ta
您需要注册该单元以便重新使用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as! CustomTableViewCell
let text = data[indexPath.row]
cell.label.text = text
return cell
}
或者在故事板中输入重用标识符,方法是选择单元格,然后在右侧的属性中输入重用标识符。您需要注册单元格以进行重用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as! CustomTableViewCell
let text = data[indexPath.row]
cell.label.text = text
return cell
}
或者在故事板中输入重用标识符,方法是选择单元格,然后在右侧的属性中输入重用标识符。仅仅因为
tableView。默认情况下,dequeueReusableCell(withIdentifier:“cell”)
为nil
尝试打印时,任何可选项的情况都是相同的,例如:
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "locCell")
导致获得:
因此,通过将常数声明为:
let optionalString: String? = ""
print(optionalString)
由于返回可选的UITableViewCell
实例,cell
的类型将是UITableViewCell?
(可选的UITableViewCell
),这就是您看到此错误的原因
如何摆脱它?
假设已为单元格设置了正确的单元格标识符:
如果有自定义单元格,可以将其转换为:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
如果没有自定义单元格,则只需将
作为?MyCustomCell
向下转换删除即可。原因很简单,因为tableView.dequeueReusableCell(带标识符:“cell”)
默认为nil
尝试打印时,任何可选项的情况都是相同的,例如:
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "locCell")
导致获得:
因此,通过将常数声明为:
let optionalString: String? = ""
print(optionalString)
由于返回可选的UITableViewCell
实例,cell
的类型将是UITableViewCell?
(可选的UITableViewCell
),这就是您看到此错误的原因
如何摆脱它?
假设已为单元格设置了正确的单元格标识符:
如果有自定义单元格,可以将其转换为:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
如果没有自定义单元格,则只需将
as?MyCustomCell
向下转换删除。替换
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? MyCustomCell else {
// something goes wrong
return UITableViewCell()
}
print(cell) // it would be fine for now
// ...
return cell
}
使用此代码:
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell")
更换
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? MyCustomCell else {
// something goes wrong
return UITableViewCell()
}
print(cell) // it would be fine for now
// ...
return cell
}
使用此代码:
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell")
注意:在故事板集合tableView委托中,DataSource和set cell ID cellReuseIdentifier
注意:在故事板集合tableView委托中,DataSource和set cell ID cellReuseIdentifier。这与注册单元格无关,因为单元格已经在表格视图中-如第二个屏幕截图所示-(不是单独的.xib文件)无需执行此类实现;这是关于作为可选单元的问题,您可以检查。单元需要有一个重用标识符才能出列,添加单元不会自动执行此操作毫无疑问!假设OP是从IB builder执行的,但这不是显示警告的直接原因。警告是因为
print
接受一个any参数,并将其传递给一个UITableVIewCell?
。它与注册单元格无关,因为单元格已经在表视图中-如第二个屏幕截图所示-(不是单独的.xib文件)无需执行此类实现;这是关于作为可选单元的问题,您可以检查。单元需要有一个重用标识符才能出列,添加单元不会自动执行此操作毫无疑问!假设OP是从IB builder执行的,但这不是显示警告的直接原因。警告是因为print
接受一个any参数并将其传递给一个UITableVIewCell?
。您好:)但它只会转到某个出错的部分…即使我添加了标识符locCell…并且不能像下面那样设置文本。print(“设置tablet值”)print(cell.name)print(“----------”)print(row.name)cell.name?.text=row.name!因为cell.name为Null,所以不会收到任何错误消息,因为在您编写的上述代码中,如果出现问题,它只会返回。所以列表表视图显示为blankhello:)但它只会转到出错的部分…即使我添加了标识符locCell…并且不能像下面那样设置文本。print(“设置tablet值”)print(cell.name)print(“----------”)print(row.name)cell.name?.text=row.name!因为cell.name为Null,所以不会收到任何错误消息,因为在您编写的上述代码中,如果出现问题,它只会返回。因此,列表表视图显示为blankI get错误消息***由于未捕获的异常“NSInternalInconsistencyException”终止应用程序,原因:“无法将标识符为locCell的单元格出列-必须为标识符注册nib或类,或在情节提要中连接原型单元格”为什么?我在主情节提要上添加了locCell标识符…我添加了我如何在情节提要上添加标识符的图片…你们能检查一下它是否正确吗。您是否设置了UITableViewCell类名和单元Id?我收到错误消息***由于未捕获的异常“NSInternalInconsistencyException”终止应用程序,原因是:“无法将标识符为locCell的单元出列-必须为标识符注册nib或类,或在情节提要中连接原型单元”为什么?我在主情节提要上添加了locCell标识符…我添加了我如何在情节提要上添加标识符的图片…你们能检查一下它是否正确吗。是否设置了UITableViewCell类名和单元Id?