我使用自定义标题视图和委托方法 tableView:viewForHeaderInSection: 获得了我想要的外观非常成功。但我认为它正在产生内存泄漏,我不知道该怎么办。
代码是这样的:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSLog (@"New header, section %d", section);
ResultsHeaderView *header = [[ResultsHeaderView alloc] initWithFrame:CGRectMake(0, 0, defaultResultsHeaderSize.width, defaultResultsHeaderSize.height)];
SearchResult *result = [[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0];
header.text = result.searchUsed.keywords;
header.searchTermsEntity = result.searchUsed;
header.resultDelegate = self;
header.section = section;
return [header autorelease];
}
可以看到,每次调用this时,都会实例化一个ResultHeaderView类型的新对象,它是UIView的子类。
问题是它经常被调用,每次一个节标题从视图中滚动出来然后又重新打开时,它就会被调用。添加新部分时,它会被多次调用,即使对于其他部分也是如此(尽管我可能对此有一些控制,我将对其进行研究。)
我想知道是否有类似 tableView:dequeueReusableCellWithIdentifier: 可以管理节标题视图,或者知道何时需要发布节标题视图的方法。我不确定自动释放是否足以避免泄漏。
同时,我的理解是创建单元格的成本很高,这就是为什么它们可以通过 dequeueReusableCellWithIdentifier 过程重用。我不得不想象这与节标题相同。
有谁在评论之前遇到过这个问题吗?