Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6363913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:05:52+00:00 2026-05-25T00:05:52+00:00

I am having great success getting the look I want using a custom header

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:05:53+00:00Added an answer on May 25, 2026 at 12:05 am

    Here is what I have decided to do, unless someone can see a flaw in it or can come up with a better idea.

    In the view controller that manages the tableView, I added a property

    NSMutableArray *viewsForSectionHeaders;
    

    Then I have modified my tableView:viewForHeaderInSection:

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        SearchResult *result = [[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0];
    
        ResultsHeaderView *header;
    
        if (section < [viewsForSectionHeaders count]) {
            header = [viewsForSectionHeaders objectAtIndex:section];
        }
        else {
            header = [[[ResultsHeaderView alloc] initWithFrame:CGRectMake(0, 0, defaultResultsHeaderSize.width, defaultResultsHeaderSize.height)] autorelease];
            [viewsForSectionHeaders addObject:header];
        }
    
        header.text = result.searchUsed.keywords;
        header.searchTermsEntity = result.searchUsed;
        header.resultDelegate = self;
        header.section = section;
    
        return header;
    }
    

    In this case, I have an array of header views. It’s not immediately obvious, but whenever the fetchedResultsController is updated, it may “mismatch” the previously associated headers with different sections than they were originally matched to. But this doesn’t really matter because the header properties (text, searchTermsEntity, resultDelegate, and section) are all reset in the code above.

    So, until I see something I missed, this looks like it serves the purpose of only instantiating header views when they are needed, and reusing those that have already been created.

    (Note that I moved the autorelease inside where the view is allocated. I had an error where I was referring to a nil object, and this fixed it. Also, now that I think of it, havint the autorelease in the return statement would have repeated the autorelease on an object multiple times if it had been instantiated in a previous call to this code. I don’t know if that is a real problem, but it was easy to straighten that out, while I was at it.)

    Right now, I don’t delete sections, so I don’t have to worry about releasing header views. But if I did delete sections, then that means that a header view should be removed from the viewsForSectionHeaders array, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using XSLT and having great success, just got a couple of problems. Warning:
I am having great success with git push --mirror to make backup copies to
Good morning! I'm having great success so far with CodeIgniter. I'm new to PHP
I'm having great problems with getting subactivities to work properly. I've read several tutorials
I just recently got into using vim, and am having great fun adding load
I am having great difficulty reading the API for named scopes. Each bid has
We're having great results with the Phusion stack ( Passenger and Ruby Enterprise Edition
Obviously having whitespace in css, aspx and html pages is a great advantage at
Having a problem getting a TreeView control to display node images. The code below
Greetings! I'm having a problem getting a text value of a captcha from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.