简体   繁体   English

情节提要中的原型单元未创建UILabels等

[英]Prototype cell from storyboard not creating UILabels, etc

My question is very similar to this one . 我的问题与这一问题非常相似。
My custom prototype cells designed in Interface Builder using storyboards have all the internal objects set to nil (not created), so when I try to assign values to them, they remain blank. 我在使用Interface Builder使用情节提要板设计的自定义原型单元中,所有内部对象均设置为nil(未创建),因此当我尝试为其分配值时,它们保持空白。

I am using Xcode 4.6.2, and running the code in the 6.1 iPhone simulator. 我正在使用Xcode 4.6.2,并在6.1 iPhone模拟器中运行代码。 Here is what I've done: 这是我所做的:

  • designed a prototype cell in interface builder with the necessary fields. 在界面生成器中设计了具有必要字段的原型单元。
  • created a subclass of UITableViewCell for this custom cell in code, and then set the cell in interface builder to this type. 在代码中为此自定义单元格创建了UITableViewCell的子类,然后在界面构建器中将该单元格设置为此类型。
  • control-dragged the fields into the .h file, which set up the objects (UILabels, etc) for me. 控件将字段拖到.h文件中,该文件为我设置了对象(UILabel等)。 I set the identifier of the cell to "serverNameCell" 我将单元格的标识符设置为“ serverNameCell”
  • set the datasource & delegate of the table to the Viewcontroller that the table is in. 将表的数据源和委托设置为表所在的Viewcontroller。
  • associated the table with a table object in the ViewControler 将表与ViewControler中的表对象相关联

At the moment, the table displays with the correct number of sections & rows, but the values of the cell are not being set. 目前,表格中显示的分区和行数正确,但是未设置单元格的值。

This is the cellForRowAtIndexPath:(NSIndexPath *)indexPath code: 这是cellForRowAtIndexPath:(NSIndexPath *)indexPath代码:

NewServerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serverNameCell"];

I always get back a cell, and the memory location seems to be ok for a valid object. 我总是拿回一个单元格,对于有效的对象来说,内存位置似乎还可以。

But when I try to cell.name.text = [thisServer name]; 但是当我尝试cell.name.text = [thisServer name]; I find that the cell.name label is always nil. 我发现cell.name标签始终为nil。
There are no crashes - but my fields are not being set. 没有崩溃-但没有设置我的字段。

I have checked a million times that the identifier is ok - it is! 我已经检查了一百万次,确定标识符正确-是的! I have gone through the Apple documentation for custom cells from a storyboard - but still get this frustrating issue.. 我已经遍历了Apple文档中有关情节提要的自定义单元的内容-但仍然遇到了令人沮丧的问题。

Anyone else had this, and resolved it? 还有其他人解决了吗?

I had this exact problem today with Xcode 5's storyboard. 今天,我在Xcode 5的故事板上遇到了这个确切的问题。 After pulling almost all my remaining hair out, I noticed I was calling this in viewDidLoad : 拔出几乎所有剩余的头发后,我注意到我在viewDidLoad中称呼它:

[self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"Setting"];

Whoops! 哎呀! This line is unnecessary when using prototype cells in storyboards. 在情节提要中使用原型单元时,不需要此行。 The storyboard automatically configures this for me. 情节提要自动为我配置。 SwitchTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Setting" forIndexPath:indexPath]; works just fine without calling registerClass:: explicitly. 无需显式调用registerClass::即可正常工作。

The question still remains of why calling registerClass:: causes some outlets to not be connected. 问题仍然存在, 为什么调用registerClass::会导致某些插座无法连接。

An easy workaround for this problem is to tag the UILabel in the prototype and manipulate it with: 解决此问题的一个简单方法是在原型中标记UILabel并使用以下方法进行操作:

UILabel* label = [cell viewWithTag:cellTag];
label.text = [thisServer name]

Here is the most obviously missed reason ( I do it all the time! ): 这是最明显的遗漏原因( 我一直都这样做! ):

Did you set the delegate and datasource of your tableview to the controller? 您是否将表视图的委托和数据源设置为控制器? (ie self) (即自我)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

Hope you have already got a fix for this issue. 希望您已经解决了此问题。 Anyways since I do not see a straight forward answer to your issue, I am writing this down. 无论如何,由于我看不到您问题的直接答案,因此我将其写下来。

From my experience in the case of using custom Prototype cell in storyboards, provide the indexPath as well. 根据我在情节提要中使用自定义Prototype单元的经验,还提供indexPath。 Use the following method: 使用以下方法:

MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];

instead of this code: 而不是此代码:

NewServerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serverNameCell"];

Changing this will readily fix this issue. 更改此设置将很容易解决此问题。 You will see all hooked up labels and image views with their values visible on the tableview. 您将看到所有已连接的标签和图像视图,其值在表格视图中可见。

Scratched my head way too hard on this one. 在这一个上,我的头太用力了。 Ended up being that I was declaring my IBOutlets with retain instead of strong in my CustomUITableViewCell.h file. 结束了,我是说出我的IBOutlets有retain ,而不是strong在我CustomUITableViewCell.h文件。 I guess I need to research more into memory management. 我想我需要对内存管理进行更多研究。

Wrong: 错误:

@property (nonatomic, retain) IBOutlet UIImageView  *preview;

Correct: 正确:

@property (nonatomic, strong) IBOutlet UIImageView  *preview;

check how you`re initializing your TableViewController. 检查您如何初始化TableViewController。 I had the same problem, then I figured that I was not referencing my TableViewController appropriately. 我有同样的问题,然后我发现我没有适当地引用我的TableViewController。

So i had to change my code before the "pushViewController". 因此,我必须在“ pushViewController”之前更改代码。

Instead of : [[TableViewController alloc]init] use : [self.storyboard instantiateViewControllerWithIdentifier:yourTableViewIdentifier] 代替: [[TableViewController alloc]init]使用: [self.storyboard instantiateViewControllerWithIdentifier:yourTableViewIdentifier]

and dont forget to REMOVE any code registering cell classes for your tableview like : [self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"Setting"]; 并且不要忘记为您的tableview删除注册单元格类的任何代码,例如: [self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"Setting"];

Hope it helps 希望能帮助到你

Some stuff to try: 一些可以尝试的东西:

It sounds like you know what you're doing but I'll mention the obvious just to remind you. 听起来您知道自己在做什么,但我只想提醒您一点。 Make sure the "name" property is set to IBOutlet. 确保“名称”属性设置为IBOutlet。

  • In IB you can set the class type for files owner, Set this to your custom class. 在IB中,您可以为文件所有者设置类类型,将其设置为自定义类。

  • Just for sake of troubleshooting take out the dequeue stuff temporarily and try it. 只是为了进行故障排除,请暂时取出出队的东西并尝试一下。

  • Try another property name other than "name" I dont believe "name" exists in baseclass though. 尝试使用除“名称”以外的其他属性名称,但我不相信基类中存在“名称”。

I've run into similar things like this before, it's probably something simple. 我以前遇到过类似的事情,这可能很简单。

I also had the exact same problem (labels were nil in the cell). 我也有完全相同的问题(单元中的标签为nil )。

My remedy was to remove an initWithCoder method that I "accidentally" added to the table view while trying out other stuff. 我的补救措施是删除尝试其他操作时“意外”添加到表视图的initWithCoder方法。 This method is called when the cell gets initiated from the storyboard. 从情节提要中启动单元时,将调用此方法。 If you overwrite it and don't take care of initiating all labels inside of it yourself you will end up with nil labels. 如果您覆盖它,而自己却不小心启动其中的所有标签,那么最终将得到nil标签。

So, as one potential error for this symptom: Check for presence of this method. 因此,作为此症状的一个潜在错误:检查是否存在此方法。

-(id)initWithCoder:(NSCoder *)decoder {
    self = [self init];
    return self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM