g_fred
g_fred

Reputation: 6028

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:

Could someone explain:

Upvotes: 0

Views: 635

Answers (2)

bbum
bbum

Reputation: 162722

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.

Upvotes: 3

g_fred
g_fred

Reputation: 6028

Ooops, made a mistake, in (3)

In (3) I was calling [self.itemCell retainCount] to view the retain count but since self.itemCell was set to nil already , all I was getting was 0 obviously!! Not sure how I missed that...

in (3), cell retain count is 1 which is normal (the cell is retained by the array returned by loadNibNamed:owner:options:)

Upvotes: 0

Related Questions