【发布时间】:2013-03-31 20:16:41
【问题描述】:
仍然无法访问要加载到自定义单元格的变量。我有一个名为 InSeasonCell 的 xib、.h、.m。我有一个 IBOutlet InSeasonCell *_cell;在 rootviewcontroller 的 .h 中。访问我的 productAtIndex 值时出现 lldb 错误。任何帮助都会很棒。有人告诉我阅读 Table View Programming Guide。
我创造
_dataController = [[InSeasonProductDataController alloc] init];
它在 rootviewcontroller 中是 init,但传递给另一个对象,该对象用 Product 对象填充它。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"InSeasonCell";
InSeasonCell *cell = (InSeasonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = _cell;
_cell = nil;
}
if(_dataController!=nil){
Product *productAtIndex = [_dataController objectInListAtIndex:indexPath.row];
// Configure the cell...
cell.name.text = productAtIndex.name;
cell.week.text = productAtIndex.week;
cell.image.image = productAtIndex.image;
}
return cell;
}
我查看了我的代码,发现我正在将一个在一个类中创建的对象传递给另一个类。例如:
-(id)initWithDataController:(InSeasonProductDataController *)dataController spinner: (UIActivityIndicatorView *)spinner{
if(self = [super init]){
_dataController = [dataController retain];
_spinner = [spinner retain];
}
return self;
}
_dataController 稍后在此方法中使用 Product 对象填充并被释放。
【问题讨论】:
-
这与您之前的问题stackoverflow.com/questions/15726970/… 有什么不同,您已经接受了答案?
-
请注意,使用
registerNib:...是加载自定义表格视图单元格的一种更简单的方法,请参见例如stackoverflow.com/a/15591474/1187415. -
好的。谢谢,我去看看。
-
尝试使用示例,但我得到 -[UITableView registerNib:forCellWithReuseIdentifier:]: unrecognized selector sent to instance。我会继续寻找
-
registerNib:...仅适用于 iOS >= 5,也许这就是问题所在。
标签: objective-c xcode memory-management lldb