【发布时间】:2011-05-19 20:32:48
【问题描述】:
我刚刚添加了一些取自 Apple docs 的代码,它显示了如何使用从 nib 创建的自定义 UITableViewCells。我是 iOS 开发的新手,所以我仍在学习正确的内存管理,而且代码对我来说并不完全清楚它是如何工作的。当我运行 Allocations 工具时,它会在下面的代码中显示未分配的内存。具体来说,它显示在视图控制器从导航堆栈中弹出后 UITableViewCells 仍然存在...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPhone" owner:self options:nil]; //<--Allocations instrument complaining about this line
cell = customCell;
[self setCustomCell:nil];
}
return cell;
}
customCell 是这个视图控制器中定义的一个实例变量,像这样...
IBOutlet UITableViewCell *customCell;
@property (nonatomic, retain) IBOutlet UITableViewCell *customCell;
这段代码有问题吗?
非常感谢您的智慧!
【问题讨论】:
-
希望你也定义了一个名为 customCell 的@property?
-
自定义 Cell 是否需要成为 IBOutlet?如果您只是想加载自定义销售,则不需要将其作为出口。
-
DerekH,它需要是一个插座,这样他就可以在加载笔尖时连接电池上的手柄。
-
澄清一下,问题是你泄露了这些单元格? “未分配”是什么意思?
-
@Firoze,在 Allocations 工具中,它显示我的每个 UITableViewCells 在表格视图控制器从导航堆栈中弹出后仍然保持活动状态,这意味着我的内存使用量每次都会增长这么多内存视图控制器已加载。它在上面标记的代码行旁边显示 95%。我假设这意味着 95% 的问题源于那行代码?
标签: ios objective-c iphone ipad memory-management