【问题标题】:iOS 5 cellForRowAtIndexPath Memory LeakiOS 5 cellForRowAtIndexPath 内存泄漏
【发布时间】:2012-03-04 03:26:30
【问题描述】:

我在使用 Instruments 时一直在设备 (iOS 5) 上测试该应用程序,但我发现了一些内存泄漏。

这是我从 Instruments 重定向到的代码部分(请参阅箭头以了解确切的行):

- (UITableViewCell *)tableView:(UITableView *)tableView
                         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      CeldaUltimasFotosViewCell *cell =
          (CeldaUltimasFotosViewCell *) [self.tableView 
                 dequeueReusableCellWithIdentifier:@"CeldaUltimasFotosViewCell"];

      if (cell == nil) {
- - - - > NSArray *topLevelObjects =
                       [[NSBundle mainBundle] 
                             loadNibNamed:@"CeldaUltimasFotosViewCell"
                                    owner:nil options:nil];
          cell = [topLevelObjects objectAtIndex:0];
      }

      // Configure the cell...
      [[cell titulo] setFont:fuente_titulo];
      ...
      return cell;
}

如您所见,我有一个从 NIB 文件加载的自定义单元格。单元格有三个文件(customCell.mcustomCell.hcustomCell.xib)。问题是我不知道我是否必须在单元控制器中释放某些东西(现在是空的,没有方法),因为这是带有 ARC 的 iOS 5。

【问题讨论】:

    标签: ios memory-leaks uitableview automatic-ref-counting


    【解决方案1】:

    在这里查看我的答案:

    How can I recycle UITableViewCell objects created from a XIB?

    你甚至不需要在 iOS5 上使用 loadNibNamed

    【讨论】:

      【解决方案2】:

      查看 Table View Programming 以及如何从 NIB (XIB) 文件中加载单元格。

      https://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

      首先奇怪的是,您将单元格存储在局部变量中。您应该将自定义单元格连接到类中的一个属性,并且您在代码中调用的所有内容是:

      [[NSBundle mainBundle] loadNibNamed:@"CeldaUltimasFotosViewCell" owner:self options:nil];
      

      遵循从 Nib 文件加载自定义表格视图单元格中的代码,您不会出错。

      【讨论】:

      • 您好,twilson,感谢您的建议。不幸的是,我已经阅读了这些文件一段时间,但没有成功。我在 Stackoverflow 中阅读了这个答案后去了那里:link。无论如何,我会重新经历一遍,以防我错过了什么。如果你在我的代码中看到一些奇怪的东西,请告诉我。如果我找到解决方案,我会发布。再次感谢!
      • 我在答案中添加了更多细节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多