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

  • SEARCH
  • Home
  • 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 7576569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:57:14+00:00 2026-05-30T16:57:14+00:00

I have an application with navigation controller and some table view controller. In table

  • 0

I have an application with navigation controller and some table view controller. In table view controller I have a two section header my definitions:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

  if (section == 0) {

   UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 74)];

        UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbarTopBack.png"]];

        UILabel *headline = [[UILabel alloc] initWithFrame:CGRectMake(11, 14, 305, 21)];
        headline.backgroundColor = [UIColor clearColor];
        headline.textColor = [UIColor whiteColor];
        headline.font = [UIFont fontWithName:@"Helvetica Neue" size:21];
        headline.text = searchPosition;

        UILabel *subHeadline = [[UILabel alloc] initWithFrame:CGRectMake(11, 36, 305, 21)];
        subHeadline.backgroundColor = [UIColor clearColor];
        subHeadline.textColor = [UIColor grayColor];
        subHeadline.font = [UIFont fontWithName:@"Helvetica Neue" size:16];
        subHeadline.text = searchRegion;

        [customView addSubview:myImageView];
        [customView addSubview:headline];
        [customView addSubview:subHeadline];


        return customView;

    } else {

        // create the parent view that will hold header Label
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
        customView.userInteractionEnabled = YES;

        UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainToolBar.png"]];

        UIToolbar *topToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
        topToolbar.barStyle = UIBarStyleDefault;
        [topToolbar setBackgroundColor:[UIColor clearColor]];

        static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{

        NSMutableDictionary *appSettingsData = [[NSMutableDictionary alloc] initWithDictionary:[appDelegate getAppStrings]];

        NSArray *segmentItems = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@ (%d)", [appSettingsData valueForKey:@"segmentButton4"], listCountOffers], [NSString stringWithFormat:@"%@ (%d)", [appSettingsData valueForKey:@"segmentButton5"], [[appDelegate comunication] getSimilarCount:[appDelegate getCurrentCI] idPosition:idPosition idRegion:idRegion]], nil];

        segmentControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
        segmentControl.frame = CGRectMake(6, 8, 308, 29);
        segmentControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentControl setTintColor:[UIColor grayColor]];
        [segmentControl addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
        segmentControl.momentary = NO;
        segmentControl.selectedSegmentIndex = 0;

        });

        UIBarButtonItem *toolbarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
        [topToolbar addSubview:toolbarItem.customView];

        [customView addSubview:myImageView];
        [customView addSubview:topToolbar];

        return customView;

    }

}

I use “static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{“, because I need create this header only first time (because when I scroll table, method is call and call … and this is wrong) …
Everything works fine, when I create table view controller it show a headers, when I scroll it, nothing is recreating (it is fine), but when I push back button a then reopen tableview controller headers are empty. Where is problem ? Is there any solution, how to fix it ? Thanks a lot

  • 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-30T16:57:16+00:00Added an answer on May 30, 2026 at 4:57 pm

    You’re using dispatch_once without really needing to. The block will only be executed once, even if you subsequently remove the view controller from memory and deallocate segmentControl.

    Use lazy loading instead – create a property for your segmentControl view within your view controller, and in the accessor for that, if the backing ivar is nil, create it then:

    Your synthesize statement:

    @synthesize segmentControl = _segmentControl
    

    Your accessor method:

    -(UISegmentedControl*)segmentControl
    {
        if (_segmentControl)
            return _segmentControl;
    
        UISegmentedControl *segmentControl = //... create your control here 
        self.segmentControl = segmentControl
        return segmentControl;
    }
    

    Then when you want to use the view, use self.segmentControl. The first time you call it, it will be created, the subsequent times, it will be re-used.

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

Sidebar

Related Questions

In My Application 1) Login View 2) -View -Tab View Controller -Navigation Caontroller -Table
I have a tabbed application with navigation controllers in tabs and view controller in
I have a tabbed application with navigation controllers in tabs and view controller in
I have a Navigation Controller with two Views. The First View is a ViewController
I have a Tabbar application with a navigation controller (classic). At some point when
I have a tab-bar and navigation controller application (like Youtube app or Contacts app).
I have a core data application which uses a navigation controller to drill down
I have a Silverlight navigation application, that for some reason when showing a data
I have a UITabBar in the detail view of my navigation based application. I
I'm developing a graphing application that on the main navigation/tab view displays a UIView

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.