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 6595637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:57:56+00:00 2026-05-25T17:57:56+00:00

I have some problem implementing a segment control. Because i want it to be

  • 0

I have some problem implementing a segment control. Because i want it to be a fixed header so when i scroll i can always see it, i’ve implemented it in the

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

All is well till here, the segment control appears. The problem is when the segments are clicked. Although the function implemented with a selector is called and the segment control has the correct selectedSegmentIndex, the segments are not highlighted except the one that is initially set with the

sortControl.selectedSegmentIndex = 0; in the viewForHeaderInSection . This Segment interacts being highlighted and non-highlighted (when pressed again). Another weird thing is that when i press the other segments, the segment at 0 becomes highlighted.

Here is the complete code for the viewForHeaderInSection :

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

     UIColor *tintColor = [UIColor colorWithRed:241.0/255 green:78.0/255 blue:35.0/255 alpha:1];



    sortControl = [[UISegmentedControl alloc] initWithItems:
                   [NSArray arrayWithObjects:@"Distance", @"Rating", @"Name", nil]];
    sortControl.segmentedControlStyle = UISegmentedControlStyleBar;

        sortControl.tintColor = tintColor;
    sortControl.frame = CGRectMake(20, 20, 280, 35);
    sortControl.selectedSegmentIndex = 0;
    [sortControl addTarget:self action:@selector(sortChanged) forControlEvents:UIControlEventValueChanged];

        UIView *view=[UIView new];
        view.frame = CGRectMake(0, 0, 320, 70);
        view.backgroundColor =[UIColor blueColor];
        [sortControl setEnabled:YES forSegmentAtIndex:0];
        [sortControl setEnabled:YES forSegmentAtIndex:1];
        [sortControl setEnabled:YES forSegmentAtIndex:2];


        view.userInteractionEnabled = YES;

        [view addSubview:sortControl];




        return view;

    }
  • 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-25T17:57:57+00:00Added an answer on May 25, 2026 at 5:57 pm

    You have here two kind of problems:
    the first one has been arisen by AliSoftware in his previous response: you must set the momentary property to NO to avoid the flickering issue.

    But the main problem is in the way you use the

    tableView:viewForHeaderInSection:

    delegate method. In this method you are recreating each time the same view, with two bad effects:
    – the first is visible in your app: the segmented control is re-initialized and the selected button is set to the first one (index 0)
    – the second is adding a memory leak each time the method is called by the table view. Consider that this method is called multiple times by the table view and out of your control: essentially each time the header is scrolled outside the screen and then it re-enters, the table needs to regenerate the view and calls the method again. On your code the segment is created but never released thus leaking.

    The solution to this problem is to define a single instance for the header, set it initially to nil and then check if it is nil or not. If nil create it, if not use the previously generated instance.
    Another possible way to do this is in the code below. So create a static instance and use GCD’s dispatch_once to create the segmented control only the first time. In this case you will never lose the current control status as it will be reused at each header call. You can improve the performance by also moving the whole header UIView creation inside the dispatch_once block, so avoiding extra allocs each time.

    
    
    static UISegmentedControl *sortControl;
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        UIColor *tintColor = [UIColor colorWithRed:241.0/255 green:78.0/255 blue:35.0/255 alpha:1];
    
    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            sortControl = [[UISegmentedControl alloc] initWithItems:
                            [NSArray arrayWithObjects:@"Distance", @"Rating", @"Name", nil]];
            sortControl.segmentedControlStyle = UISegmentedControlStyleBar;
    
            sortControl.tintColor = tintColor;
            sortControl.frame = CGRectMake(20, 20, 280, 35);
            sortControl.selectedSegmentIndex = 0;
    
        });
    
        [sortControl addTarget:self action:@selector(sortChanged) forControlEvents:UIControlEventValueChanged];
    
        UIView *view=[UIView new];
        view.frame = CGRectMake(0, 0, 320, 70);
        view.backgroundColor =[UIColor blueColor];
        sortControl.momentary = NO;
        [sortControl setEnabled:YES forSegmentAtIndex:0];
        [sortControl setEnabled:YES forSegmentAtIndex:1];
        [sortControl setEnabled:YES forSegmentAtIndex:2];
    
    
        view.userInteractionEnabled = YES;
    
        [view addSubview:sortControl];
    
    
    
    
        return view;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a little problem implementing some serialization/deserialization logic. I have several classes that
I have got some problem implementing bullet physics into my opengl game. The thing
i have got some problem on implementing Django sessions. I have an employee listing
I have following problem. I have some unit tests implemented in a foreign assebly
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with Jquery Autocomplete plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ ) I got it
I have some problem with my .htaccess file. Here I am adding my problem.
I have some problem to order an array by a field of this, here
I have some problem whith such mysql_query INSERT INTO table VALUES ('', CURDATE()-1) why
I have some problem's with a simple application in JSF 2.0. I try to

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.