我创建了一个名为 CustomTableViewCell
的自定义 UITableViewCell
并具有 xib。
CustomTableViewCell
的代码如下-
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
}
在 UITableViewController
中,我按如下方式使用 CustomTableViewCell
-
在
viewDidLoad()
中,调用了register(_:forCellReuseIdentifier:)
。在
tableView(_:cellForRowAt:)
函数中,dequeueReusableCell(withIdentifier:for:)
被调用,label1< 的文本
已设置。
代码按预期运行,CustomTableviewCell
在应用运行时可见。
xib 还有一个自定义的UIViewController
。对于这个 xib 文件,我想将 CustomTableViewCell
添加为 subview 。
我向 xib 添加了一个 UIView
并将其类设置为 CustomTableViewCell
。当应用程序运行时,应用程序崩溃,因为 label
IBOutlet 为 nil。
谁能指出如何解决这个问题?
最佳答案
要添加自定义 UIViewcell ,您可以使用 xib 创建 uiviewcell 并为特定单元格提供可重复使用的标识符。然后在 uiviewcontroller 中定义 tableviewcell 时使用该特定 uiview 单元格。
关于ios - 如何在没有 UITableView 的情况下将自定义 UITableViewCell 与 xib 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909018/