I just added some code that was taken from the Apple docs which shows how to use custom UITableViewCells that are created from a nib. I’m new to iOS development, so I’m still learning about proper memory management and the code isn’t exactly clear to me as to how it works in the first place. When I run the Allocations instrument, it shows unallocated memory in the code below. Specifically, it shows the UITableViewCells remain alive after the view controller is popped off the nav stack…
- (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 is an instance var defined in this view controller, like so…
IBOutlet UITableViewCell *customCell;
@property (nonatomic, retain) IBOutlet UITableViewCell *customCell;
Is there a problem in this code?
Thanks so much for your wisdom!
I compared the code from the documentation:
With your code:
There seems to be a disparity at the line where you assign
cell. Apple’s example code usestvCell, the instance variable, whereas you use[self customCell], the property getter for the instance variable. Could that be the cause? Try changingto