I am having great success getting the look I want using a custom header view and the delegate method tableView: viewForHeaderInSection:. But I think it is producing a memory leak, and I'm not sure what to do about it.
The code is this:
- (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];
}
As you can see, every time this is called, it instantiates a new object of type ResultsHeaderView, which is a subclass of UIView.
The problem is that it is called often, every time a section header is scrolled off of the view and then back on, it gets called. It gets called multiple times when a new section is added, even for the other sections (although I may have some control over that, and I'm going to look into it.)
I am wondering if there is something like tableView:dequeueReusableCellWithIdentifier: that can manage section header views, or a way to know when a section header view is in need of a release. I am not sure if the autorelease is sufficient to avoid a leak.
At the same time, my understanding is that creating cells is costly, and that's why they get reused with the dequeueReusableCellWithIdentifier process. I have to imagine this would be the same with section headers.
Would anyone who has come across this issue before comment?