在IB中连接有引用插座时自定义UITableCellView

时间:2011-11-21 17:48:28

标签: iphone uitableview

我正在尝试使用IB中定义的自定义UITableViewCells,其中有引用插座。我已成功使用stackoverflow中几个地方显示的技术来加载和使用UITableViewClass,如果没有引用插座,如下所示。

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:nil options:nil];

我有一个名为“TheCellsClass.xib”的单独文件,它有一个UITableViewCell定义了一个名为Alabel的单个UILable,“IBOutlet UILabel * Alabel;”。如果我将标签连接到ALabel,那么我会收到此错误

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x681b360> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Alabel.'

在这里和网上搜索之后我明白这是因为“owner:nil”没有用这个对象定义一个类:Alabel。我不能使用“owner:self”,因为那是UITableViewController,也没有定义“Alabel”。

我创建了一个名为“TheCellsClass”的类作为“UITableViewCell”的子类,它定义了Alabel,见下文;

然后使用:

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];

我仍然得到同样的错误。那么,任何人都可以指出我的方式的错误吗? :-) 我只能想到解决这个问题就是删除所有引用插座并连接  他们使用代码

子类标题:

#import <UIKit/UIKit.h>
@interface TheCellsClass : UITableViewCell {   
    IBOutlet UILabel *Alabel;    
}
@property (strong, nonatomic) UILabel *Alabel;
@end

子类体:

#import "TheCellsClass.h"
@implementation TheCellsClass
@synthesize Alabel;
@end

在表格视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我正在使用:

TheCellsClass* cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];

示例项目的zip是http://www.proaa.com/tryout.zip

连连呢?请求更多信息?

任何帮助表示赞赏。 杰夫

2 个答案:

答案 0 :(得分:0)

如果不看你的项目,我会想到一件事。如果您的笔尖中有自定义单元格的实例,然后在同一个笔尖中有此标签,则连接应该是从UILabel到自定义单元格。你不应该把它连接到文件的所有者,至少从你描述你想要达到的目的的方式来看。

希望有所帮助。

答案 1 :(得分:0)

三件事:

  1. 从DetailViewController.xib中删除TheCellClass

  2. ALabel链接到TheCellsClass中的文件所有者 - 将其链接到Objects部分中的TheCellsClass。还要考虑重命名ALabel - 标准实例变量是以小写字母开头的标准。

  3. 在MasterViewController中,更改

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:cell options:nil];
    
  4.     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TheCellsClass" owner:nil options:nil];
    

    经过这三次更改后,您的自定义TableViewCell出现在模拟器中。还要考虑将TheCellsClass重命名为暗示其子类的内容,例如MyCustomTableViewCell。

相关问题