在Interface Builder中自定义UITableViewCell

时间:2012-01-28 06:53:55

标签: iphone ios uitableview

在两个类中,我已经将UITableViewCell子类化,以便进行一些主要的自定义。我想使用Xib文件将UI布局代码的数量保持在最低限度。我遇到了一个奇怪的例外:

if (!cell) {
    if (indexPath.row == 0) {
        cell = [[[SearchCellTop alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"SearchCellTop" owner:cell options:nil];
        cell = (SearchCellTop*)[objects objectAtIndex:0];
    }
    else {
        cell = [[[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:cell options:nil];
        cell = (SearchCell*)[objects objectAtIndex:0];
    }
}

这似乎适用于加载Xibs。但是,只要我尝试做以下事情:

if (indexPath.row < [self tableView:tableView numberOfRowsInSection:indexPath.section])
    ((SearchCell*)cell).Product = [products objectAtIndex:indexPath.row];

我得到 - [UIAccessibiltyBundle setProduct:]无法识别的选择器发送到实例

所有内容都表明'cell'的类型正确,但我仍然会收到此错误。

1 个答案:

答案 0 :(得分:0)

来自Apple的+ (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner方法的开发人员文档:

  

所有者

     
    

要指定为nib FIle所有者的对象。如果此对象的类具有关联的包,则搜索该包以查找指定的nib文件;否则,此方法将在主捆绑中查找。

  

在您的情况下,所有者应为零(或特定的捆绑,如果相关)。

在代码中,将调用更改为loadNibNamed方法,如下所示:

NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"SearchCellTop" owner:nil options:nil];

NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];