iphone - 在 Interface Builder 中自定义 UITableViewCell

标签 iphone ios uitableview

在两个类中,我对 UITableViewCell 进行了子类化,以便进行一些主要的定制。我想使用 Xib 文件将 UI 布局代码的数量保持在最低限度。我遇到了一个奇怪的异常(exception):

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:] 无法识别的选择器发送到实例

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

最佳答案

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

owner

The object to assign as the nib FIle's Owner. If the class of this object has an associated bundle, that bundle is searched for the specified nib file; otherwise, this method looks in the main bundle.

在您的情况下,所有者应该为 nil(或特定的 bundle ,如果关联的话)。

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

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

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

关于iphone - 在 Interface Builder 中自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9043549/

相关文章:

ios - FMDBBlockSQLiteCallBackFunction 在未使用 makeFunctionNamed 的应用程序中崩溃

ios - Firebase 规则 - 如何检查用户是否存在于子文档中

swift - 通过触摸该单元格内的按钮来删除 UITableViewCell

ios - 是否允许添加打开 Safari 的 UITabBarItem?

iphone - 当 UITextField 位于 UIScrollView 内时,使用点击手势关闭键盘时出现问题。

iphone - 为什么 NSNotification 第三个参数通常是对象 :nil?

ios - 设置 UITableViewCell backgroundView 和 selectedBackgroundView 在 iOS7 中不起作用

iphone - 具有多个属性的 NSXMLParser

iphone - iOS自定义表格 View 单元格在编辑模式下调整大小

ios - UILabel 应该产生省略号,但不起作用