【问题标题】:iOS: Custom Cell lldb Memory access erroriOS:自定义单元 lldb 内存访问错误
【发布时间】: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


【解决方案1】:

要解决此错误,您需要在 xib 文件中单击单元原型,然后在属性检查 > 标识符中插入与 .m 文件中相同的名称(我写下“单元”):

在方法中:

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ………… Cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; Cell = [[MainCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

【讨论】:

    【解决方案2】:

    我想通了。我的 Product 对象初始化了变量,我必须为传入的每个变量复制一份。这消除了 lldb 错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多