cspam cspam - 3 months ago 13
iOS Question

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. I am using XCode 5 but ARC is disabled. 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). Below are the sections of code that are relevant:


static 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.

Can anyone point me to a solution here? Thanks...

Answer

The problem could be that first line in tableView:cellForRowAtIndexPath:. You create a TransactionResultCell that you don't do anything with if self.isLoading evaluates to true. That line should be inside the else clause (as well as the return cell line).