开发者

UITableView retain count

开发者 https://www.devze.com 2023-01-31 13:29 出处:网络
Inside ViewWillAppear I am adding a UITableView to controller\'s view. UITableView *myTableView_portrait = [[UITableView alloc] initWithFrame:CGRectMake(0, 102, 768, 857) style:UITableViewStylePlain]

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开发者_高级运维?


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.


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.


My guess is that when this line happens:

[self.view addSubview:myTableView_potrait];

myTableView is retained by 1 by its delegate controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消