Bry*_*ris 6 uitableview xib ios uinib
我很好奇使用 xib 文件来布局 UITableViewCell 内容的正确方法。当我尝试按照在 Internet 上找到的所有步骤进行操作时,我总是得到
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<NSObject 0x10fe7d790> setValue:forUndefinedKey:]: 此类与键 statusLabel 的键值编码不兼容。”
所以这是相关的代码
@interface MyCell : UITableViewCell
@property (nonatomic,strong) IBOutlet UILabel* messageLabel;
@property (nonatomic,strong) IBOutlet UILabel* statusLabel;
Run Code Online (Sandbox Code Playgroud)
在我的 UIViewController 中,我都尝试过
-(void)viewDidLoad {
[self.tableView registerNib:[UINib nibWithNibName:@"MyCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"];
}
Run Code Online (Sandbox Code Playgroud)
或使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCellReuseID";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( !cell ) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil]
lastObject];
// or sometimes owner is nil e.g.
//cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]
lastObject];
}
// ... remainder of cell setup
return cell;
}
Run Code Online (Sandbox Code Playgroud)
除了我在标题中提到的例外,这两种方法都失败了。看来,与 owner:self 错误是因为 UIViewController 没有 IBOutlet 属性。使用 owner:nil 是因为它在内部使用了一个 NSObject,当然它也没有 IBOutlet 属性。
我发现的唯一解决方法如下。在我的单元格的 init 方法中,我将返回的视图/单元格添加到我的初始化单元格
例如
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// nil out properties
[self.contentView addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil] lastObject]];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
这看起来真的很奇怪(虽然我也可以将 xib 中的基本类型更改为 UIView 而不是 MyCell 或 UITableViewCell),这让它感觉不那么矫情。
我看过很多帖子,人们遇到了这个特定的错误。这通常被解释为是 xib 本身的接线问题,但是如果我删除 xib 中的所有连接,它会加载正常,但是当我重新添加 ui 元素和文件所有者之间的连接时,错误返回,所以我不要认为它与“清理”xib 没有任何关系(即使查看 xib 的 XML,也没有列出错误的连接)。
有没有其他人对这个错误是如何产生的有任何想法?
您是否已连接 Cell Nib 文件中“messageLabel 和 statusLabel”的插座?该错误表明 Nib 文件中未找到“statusLabel”的 IBOutlet 属性(连接问题)。
| 归档时间: |
|
| 查看次数: |
3723 次 |
| 最近记录: |