【问题标题】:Customizing UITableViewCell in Interface Builder在 Interface Builder 中自定义 UITableViewCell
【发布时间】:2012-01-28 06:53:54
【问题描述】:

在两个类中,我对 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];
    }
}

这似乎适用于加载 Xib。但是,一旦我尝试执行以下操作:

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

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

一切都表明“单元格”的类型正确,但我仍然收到此错误。

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

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

    所有者

    指定为 nib 文件所有者的对象。如果这个对象的类有一个关联的包,则在那个包中搜索指定的 nib 文件;否则,此方法会在主包中查找。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多