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