Ios 情节提要UITableViewCell无法加载名为的捆绑包中的nib。。。以编程方式加载时

Ios 情节提要UITableViewCell无法加载名为的捆绑包中的nib。。。以编程方式加载时,ios,swift,uitableview,storyboard,nib,Ios,Swift,Uitableview,Storyboard,Nib,我有一个带有UITableView的Viewcontroller场景(故事板),在一个名为Foo的UITableViewCell内Foo从故事板加载时可以正常加载,但我需要以编程方式从另一个UITableView加载它 我正在这样注册单元格(起初我尝试不使用UINib,但是IBOutlets是nil): 然后像这样加载它: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITab

我有一个带有
UITableView
的Viewcontroller场景(故事板),在一个名为Foo
UITableViewCell
Foo从故事板加载时可以正常加载,但我需要以编程方式从另一个
UITableView
加载它

我正在这样注册单元格(起初我尝试不使用
UINib
,但是
IBOutlets
nil
):

然后像这样加载它:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "bar", for: indexPath) as! Foo
    return cell
}
这不起作用并返回错误: 由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在名为“Foo”的捆绑包:“NSBundle(loaded)”中加载NIB”

我确定的事情:

  • 属性检查器中的Foo
    UITableViewCell
    标识符为Foo
  • 具有正确的目标成员资格

您可能希望为您的单元格创建一个单独的nib文件,然后像上面所做的那样将其注册到表视图中。

问题是我不使用故事板,这就是我以编程方式执行此操作的原因,如果不将
UItableView单元格
移动到
UItableView
之外,是否可以执行此操作?如果要重用表视图单元格,则应将其移动到单独的nib文件中。之后,您可以将该单元格注册到所需的任何表视图中。如果将单元格保留在序列图像板文件中视图控制器的表视图中,则无法重用它。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "bar", for: indexPath) as! Foo
    return cell
}