Search code examples
iphoneobjective-ciosuitableviewnsbundle

Retain count when loading UITableViewCell with loadNibNamed


I am using loadNibNamed:owner:options: as documented by Apple to load a custom UITableViewCell from a nib file:

ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
    // (1)
    [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
    // (2)
    cell = self.itemCell;
    self.itemCell = nil;
    // (3)
    // code continues here
}

And the class declaration of the view controller:

@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
@private
    UITableView *tableView;
    ItemCell *itemCell;
}

@property (nonatomic, retain) IBOutlet ItemCell *itemCell;

MyViewController is the File's Owner of the ItemCell.

I am observing the following:

  • (1) self.itemCell retain count is 0
  • (2) self.itemCell retain count is 2
  • (3) self.itemCell retain count is 0
  • (3) cell retain count is 1

Could someone explain:

  • Why does self.itemCell retain count go from 2 to 0 between (2) and (3)?
  • Why is the retain count of cell in (3) equal to 1?

Solution

  • retainCount is useless. Don't call it.

    As for the answer to your two questions, "implementation detail".

    As long as you balance your retains and releases, your job is done. Explaining why the retain count is any given absolute value would require access to the implementation of the frameworks themselves.