gpt4 book ai didi

iOS 开发 : How am I mismanaging memory in this code?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:19:37 26 4
gpt4 key购买 nike

我刚刚添加了一些取自 Apple docs 的代码它显示了如何使用从 Nib 创建的自定义 UITableViewCells。我是 iOS 开发的新手,所以我仍在学习适当的内存管理,而且代码对我来说并不是一开始就很清楚它是如何工作的。当我运行 Allocations 工具时,它会在下面的代码中显示未分配的内存。具体来说,它显示在 View Controller 从导航堆栈弹出后 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 是在这个 View Controller 中定义的实例变量,就像这样......

IBOutlet UITableViewCell *customCell;
@property (nonatomic, retain) IBOutlet UITableViewCell *customCell;

这段代码有问题吗?

非常感谢您的智慧!

最佳答案

我比较了文档中的代码:

if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
}

使用您的代码:

if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPhone" owner:self options:nil];
cell = [self customCell];
[self setCustomCell:nil];
}

您分配 cell 的行似乎存在差异。 Apple 的示例代码使用tvCell实例变量,而您使用[self customCell]实例变量的属性 getter 。这可能是原因吗?尝试改变

    cell = [self customCell];

    cell = customCell;

关于iOS 开发 : How am I mismanaging memory in this code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4347600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com