我创建了一个类,它是 UITableViewCell
的子类,我还为它创建了一个 xib
文件。
在 xib
文件中,我添加了一个 UITableViewCell
,然后给它一个标识符“ChatCell”,然后在我的类里面,我尝试像这样使用这个自定义单元格:
static NSString *CellIdentifier = @"ChatCell";
ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
在类(class)中,我提供了一个 UIlabel
的 IBOutlet
并将其连接到我在 中创建的
但是当创建单元格时 UILabel
之一xibUILabel
保持 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/