xcode 自定义 UITableViewCell 子类:此类不是键值编码兼容的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9713651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom UITableViewCell subclass: This class is not a key value coding-compliant
提问by acecapades
I've looked at almost all of the related questions about this here in stackoverflow and tried all their possible solutions but I still can't figure out why I get this error:
我在 stackoverflow 中查看了几乎所有与此相关的问题,并尝试了所有可能的解决方案,但我仍然无法弄清楚为什么会出现此错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CustomCell 0x6e627d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key title.'
I'm trying to load a customized table view cell with the nibname: @"CustomCell". Its class is assigned to "CustomCell" (same class name as the nib name). Its file's owner is also set to the class which loads this cells - "ProductsTableViewControllerWithSearch". All the outlets in the nib are connected to the ones in CustomCell.m
我正在尝试使用笔尖名称加载自定义表格视图单元格:@"CustomCell"。它的类被分配给“CustomCell”(与笔尖名称相同的类名)。它的文件所有者也设置为加载此单元格的类 - “ProductsTableViewControllerWithSearch”。笔尖中的所有出口都连接到 CustomCell.m 中的出口
Here's my code:
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellClassName = @"CustomCell";
static NSString *CellIdentifier = @"Custom Items Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelItems = [[UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
//... more irrelevant code here
}
Can somebody help me please. I've been working on this for more than 4 hours. Thanks a lot!
有人可以帮我吗。我已经为此工作了 4 个多小时。非常感谢!
PS: I'm using ARC and am developing for iOS 5.
PS:我正在使用 ARC 并且正在为 iOS 5 开发。
Here's my CustomCell.h.
这是我的 CustomCell.h。
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell{
UILabel *textLabel;
UILabel *detailTextLabel;
UIButton *starButton;
}
@property (strong, nonatomic) IBOutlet UILabel *textLabel;
@property (strong, nonatomic) IBOutlet UILabel *detailTextLabel;
@property (strong, nonatomic) IBOutlet UIButton *starButton;
@end
There's nothing in my CustomCell.m
我的 CustomCell.m 中什么都没有
采纳答案by JoePasq
In Interface Builder you need to set the class of the cell to CustomCell and CustomCell needs to have a property title
.
在 Interface Builder 中,您需要将单元格的类设置为 CustomCell 并且 CustomCell 需要有一个属性title
。
this class is not key value coding-compliant for the key …
this class is not key value coding-compliant for the key …
Usually means that a property is not found on a class and Interface Builder is trying to use that property, resulting in a crash.
通常意味着在类上找不到属性并且 Interface Builder 正在尝试使用该属性,从而导致崩溃。
回答by Black Tiger
This error occurs if you are linking up any thing that does not belongs to that view.
如果您正在链接不属于该视图的任何内容,则会发生此错误。
In this case, the solution is in Interface Builder
在这种情况下,解决方案是在 Interface Builder 中
- First Change the File's Owner Identity Class to NSObject.
- Unlink every IBOutlet in File's Owner.
- Change the Identity Class of Your custom NIBs to the name of CustomTableViewCell class.
- Link all your IBOutlet here.
- 首先将文件的所有者标识类更改为 NSObject。
- 取消链接文件所有者中的每个 IBOutlet。
- 将您的自定义 NIB 的标识类更改为 CustomTableViewCell 类的名称。
- 在此处链接您所有的 IBOutlet。
That's it.
就是这样。
回答by QED
I'm presuming that you get this error when your cell is loaded from the NIB at runtime. (If instead it's breaking on some line of code you'vewritten, for God's sake delete that line of code!)
我假设您在运行时从 NIB 加载单元格时会收到此错误。(如果它在您编写的某行代码上出现问题,看在上帝的份上,请删除该行代码!)
Go into your custom cell's XIB view, ctrl-click on "File's owner" and unlink whatever connection you've got in there called 'title'. If you're feeling spunky, unlink everything, then relink the stuff you want. See if that clears it up. If not I'll be here for a few hours.
进入自定义单元格的 XIB 视图,按住 Ctrl 键并单击“文件所有者”并取消链接您在那里获得的任何名为“标题”的连接。如果您感觉精神抖擞,请取消所有内容的链接,然后重新链接您想要的内容。看看这是否清除它。如果没有,我会在这里呆几个小时。
回答by Lanston
For my case of XCode6.0.1 ,I clean the project and everything runs smoothly
对于我的 XCode6.0.1 情况,我清理了项目并且一切运行顺利