里面didSelectRowAtIndexPath
我的 UITableViewController
的功能,我放置一个UIActivityIndicatorView
超过选定的UITableViewCell
因为点击此单元格可能会发出缓慢的服务器请求。我打印出 UIActivityIndicatorView
的保留计数并注意到它是 3,而我预计它是 2。
// @property (nonatomic, retain) UIActivityIndicatorView *spinner;
foo.spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge
];
UITableViewCell *cell = [self.tableView cellForRowAtIndex:indexPath];
[cell addSubView:foo.spinner];
NSLog("retain count of spinner = %d", [foo.spinner retainCount]);
第一次保留应该发生在
foo->setSpinner
.第二次保留应该发生在 tableView
将微调器添加为 subview 。第三个保留在哪里?您将如何避免这种内存泄漏?
最佳答案
您没有理由关心保留计数是多少。您应该关心的只是您的保留和释放是否平衡。
关于objective-c - 为什么在分配一个 View 并将其添加到另一个 View 后,保留计数变为 3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219659/