UITableView自定义单元格IBOutlets为零

时间:2021-05-05 22:19:31

I have created a class which is a subclass of UITableViewCell and I have also created an xib file for the same.

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

In the xib file I am adding a UITableViewCell and then giving it an identifier "ChatCell" and then in my class I am trying to use this custom cell like this:

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

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

In the class I have provided an IBOutlet of UIlabel and connected it to one of the UILabel I have created in the xib but when ever the cell gets created the UILabel stays nil.

在课堂上我提供了一个UIlabel的IBOutlet并将其连接到我在xib中创建的UILabel之一,但是当创建单元格时,UILabel保持为零。

Why is this happening?

为什么会这样?

2 个解决方案

#1


3  

You need to register your Custom Xib file in your ViewController first.

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

From Apple Docs on registerClass:forCellReuseIdentifier::

来自registerClass上的Apple Docs: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.

调用此方法或registerClass:forCellReuseIdentifier:方法告诉表视图如何创建新单元格。如果指定类型的单元当前不在重用队列中,则表视图使用提供的信息自动创建新的单元对象。

E.g. Write this line in your viewDidLoad, after setting tableView delegate and dataSource:

例如。在设置tableView委托和dataSource之后,在viewDidLoad中写下这一行:

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

If you don't, your TableView won't be able to load the Cell Nib file, which would result in nil IBOutlets of Cell.

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

#2


3  

for your xib cell may be it returns nil for your cell, write this code may be it helps you.

对于你的xib单元格可能会为你的单元格返回nil,写下这段代码可能对你有帮助。

ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

if (cell == nil)
{
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil];
    cell = [nibArray objectAtIndex:0];
}

#1


3  

You need to register your Custom Xib file in your ViewController first.

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

From Apple Docs on registerClass:forCellReuseIdentifier::

来自registerClass上的Apple Docs: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.

调用此方法或registerClass:forCellReuseIdentifier:方法告诉表视图如何创建新单元格。如果指定类型的单元当前不在重用队列中,则表视图使用提供的信息自动创建新的单元对象。

E.g. Write this line in your viewDidLoad, after setting tableView delegate and dataSource:

例如。在设置tableView委托和dataSource之后,在viewDidLoad中写下这一行:

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

If you don't, your TableView won't be able to load the Cell Nib file, which would result in nil IBOutlets of Cell.

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

#2


3  

for your xib cell may be it returns nil for your cell, write this code may be it helps you.

对于你的xib单元格可能会为你的单元格返回nil,写下这段代码可能对你有帮助。

ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

if (cell == nil)
{
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil];
    cell = [nibArray objectAtIndex:0];
}