简体   繁体   English

仪器将cellForRowAtIndexPath中的自定义表视图单元出队时显示泄漏

[英]Instruments shows leak when dequeuing Custom table view cells in cellForRowAtIndexPath

I am getting a memory leak in Instruments related to the table view delegate method cellForRowAtIndexPath when using custom table view cells. 使用自定义表格视图单元格时,在与表格视图委托方法cellForRowAtIndexPath相关的Instruments中出现内存泄漏。 I am using XCode 5 but ARC is disabled. 我正在使用XCode 5,但是ARC被禁用。 I have created my custom table view cell as a separate xib and I load that xib in my viewDidLoad method using nibWithNibName (which, if i remember, checks whether you have a cell or not so you dont have to check if cell != nil in the delegate method). 我已将我的自定义表格视图单元格创建为单独的xib,并使用nibWithNibName在我的viewDidLoad方法中加载了该xib(如果我记得的话,它会检查您是否有一个单元格,因此您不必检查cell!= nil委托方法)。 Below are the sections of code that are relevant: 以下是相关的代码部分:

static NSString *const TransactionResultCellIdentifier = @"TransactionResultCell"; 静态NSString * const TransactionResultCellIdentifier = @“ TransactionResultCell”;

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINib *cellNib = [UINib nibWithNibName:TransactionResultCellIdentifier bundle:nil];
    [self.transactionsTableView registerNib:cellNib forCellReuseIdentifier:TransactionResultCellIdentifier];

    cellNib = [UINib nibWithNibName:LoadingCellIdentifier bundle:nil];
    [self.transactionsTableView registerNib:cellNib forCellReuseIdentifier:LoadingCellIdentifier];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TransactionResultCell *cell = (TransactionResultCell *)[tableView dequeueReusableCellWithIdentifier:TransactionResultCellIdentifier];

   if(self.isLoading){
    return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
    }
   else{
    TransactionResult *transactionResult = [self.transactionResults objectAtIndex:indexPath.row];
    cell.transactionDescriptionLabel.text = [NSString stringWithFormat:@"%@: %@", transactionResult.transactionID, transactionResult.transactionDescription];
    cell.transactionPriceLabel.text = [@"Price: $" stringByAppendingString:transactionResult.transactionPrice];

    self.totalPrice += [transactionResult.transactionPrice doubleValue];
   }


return cell;
}

Instruments points me to the line up above where i am attempting to dequeue the custom table view cell along with obvious UILabel leaks that are part of the xib structure that i custom built in IB. 仪器将我指向上方的一行,在这里我试图使自定义表视图单元以及明显的UILabel泄漏出队,这些泄漏是我在IB中自定义构建的xib结构的一部分。

Can anyone point me to a solution here? 有人可以在这里指出我的解决方案吗? Thanks... 谢谢...

The problem could be that first line in tableView:cellForRowAtIndexPath: . 问题可能是tableView:cellForRowAtIndexPath:中的第一行。 You create a TransactionResultCell that you don't do anything with if self.isLoading evaluates to true. 如果self.isLoading评估为true,则创建一个不做任何事情的TransactionResultCell。 That line should be inside the else clause (as well as the return cell line). 该行应位于else子句(以及return cell行)内。

On viewWillDisappear:animated, call to nil out your nibs. 在viewWillDisappear:animated上,调用以消除笔尖。

[self.tableView registerNib:nil forCellReuseIdentifier:[WIActivityNameLabelCell reuseIdentifier]];

I have 5 custom nibs in my view, and I was losing about 0.2mb per load, which added up. 我认为我有5个自定义笔尖,每次加载时我损失了约0.2mb,这加起来了。 I suspect it is because I keep my nib and reuseIds in the actual custom table view cell, calling them as class methods. 我怀疑这是因为我在实际的自定义表格视图单元格中保留了笔尖和复用ID,并将它们称为类方法。 I think this was creating some kind of strong reference that was leaking. 我认为这正在创建某种渗漏的强大参考。 Anyway, nilling the nib solved it. 无论如何,钉住笔尖解决了它。 Note that the apple documentation states: 请注意,Apple文档指出:

If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. 如果您以前使用相同的重用标识符注册了类或nib文件,则您在nib参数中指定的nib将替换旧条目。 You may specify nil for nib if you want to unregister the nib from the specified reuse identifier. 如果要从指定的重用标识符中注销笔尖,则可以为笔尖指定nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在自定义UICollectionView中出列时返回了错误的单元格 - Wrong cells returned when dequeuing in custom UICollectionView 如何在Swift 1.2中的cellForRowAtIndexPath中返回两种类型的自定义表视图单元格之一 - how to return one of two types of custom table view cells in cellForRowAtIndexPath in Swift 1.2 如何在目标c的cellForRowAtIndexPath中返回两种类型的自定义表视图单元格之一 - how to return one of two types of custom table view cells in cellForRowAtIndexPath in objective c 即使正确调用了cellForRowAtIndexPath,UITableView也会显示空白单元格 - UITableView shows blank cells even when cellForRowAtIndexPath is called properly 未针对NIB文件中的所有自定义视图单元执行cellForRowAtIndexPath - cellForRowAtIndexPath not executing for all Custom View Cells from NIB file 当表格视图不可见时,不会调用cellForRowAtIndexPath - cellForRowAtIndexPath not being called when table view is not visible 什么时候自定义表格查看单元格dealloc? - When do custom table view cells dealloc? 具有自定义单元格的表格视图 - Table view with custom cells Xcode显示内存泄漏,但仪器没有 - Xcode shows memory leak, but Instruments does not 自定义UITableViewCell的问题:滚动出视图出队列问题时会清除UITextFields - Issue with custom UITableViewCell: UITextFields clearing when scrolled out of view- dequeuing problems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM