random
random

Reputation: 10309

UITableView retain count

Inside ViewWillAppear I am adding a UITableView to controller's view.

UITableView *myTableView_portrait = [[UITableView alloc] initWithFrame:CGRectMake(0, 102, 768, 857) style:UITableViewStylePlain];
[myTableView_portrait setTag:100];
[myTableView_portrait setDelegate:self];
[myTableView_portrait setDataSource:self];

[self.view addSubview:myTableView_potrait];
[myTableView_portrait release];

Printing retain count for this table view inside ViewWillAppear returns 1, but inside tableView: cellForRowAtIndexPath: returns 2. Even inside tableView: numberOfRowsInSection: the retain count is 1.

Any idea what is going on here?

Upvotes: 1

Views: 434

Answers (3)

Dave DeLong
Dave DeLong

Reputation: 243156

Any idea what is going on here?

Yes! What is going on here is that you're looking at -retainCount, which you should forget that you ever saw. waves hand This is not the method you're looking for.

Follow the Memory Management Guidelines and you will be fine.

Upvotes: 3

Eiko
Eiko

Reputation: 25632

You should never rely on the retain count or draw much conclusions of it - at least not for complex things like complex third party objects.

In this case the calling code might retain the table before calling your delegate method and release or autorelease it afterwards.

Upvotes: 3

Di Wu
Di Wu

Reputation: 6448

My guess is that when this line happens:

[self.view addSubview:myTableView_potrait];

myTableView is retained by 1 by its delegate controller.

Upvotes: 0

Related Questions