ios - UITableView 自定义单元格 IBOutlets 为零

标签 ios objective-c uitableview

我创建了一个类,它是 UITableViewCell 的子类,我还为它创建了一个 xib 文件。

xib 文件中,我添加了一个 UITableViewCell,然后给它一个标识符“ChatCell”,然后在我的类里面,我尝试像这样使用这个自定义单元格:

static NSString *CellIdentifier =  @"ChatCell";
ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

在类(class)中,我提供了一个 UIlabelIBOutlet 并将其连接到我在 中创建的 UILabel 之一xib 但是当创建单元格时 UILabel 保持 nil

为什么会这样?

最佳答案

您需要先在您的 ViewController 中注册您的自定义 Xib 文件。

来自 Apple Docs on registerClass:forCellReuseIdentifier: :

call this method or the registerClass:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

例如在设置 tableView 委托(delegate)和数据源之后,在 viewDidLoad 中写入这一行:

[yourTableView registerNib:[UINib nibWithNibName:@"ChatCustomCell" bundle:nil] forCellReuseIdentifier:@"ChatCell"];

如果不这样做,您的 TableView 将无法加载 Cell Nib 文件,这将导致 Cell 的 IBOutlets 为 nil。

关于ios - UITableView 自定义单元格 IBOutlets 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38973728/

相关文章:

ios - 如何从类文件中为 UIView 制作动画?

ios - GradientLayer 不水平填充 UIButton 边界

iphone - Objective-C 中的纺织品解析?

iphone - 数据源和 TableView 的问题

iOS 11 UITableView performBatchUpdates 完成关闭未被调用

ios - 使用 MFMailComposeViewController 时出现问题

ios - UITableViewCell 圆角问题

iphone - 来自ImagePicker的图像时无法检测面部特征

objective-c - 为什么这个 subview 没有被删除?

iphone - 核心数据关系、NSPredicates 和 NSFetchedResultsController