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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:27:53+00:00 2026-05-23T09:27:53+00:00

I have a UISegmentControl with my UITableView that sorts the data. I’d like to

  • 0

I have a UISegmentControl with my UITableView that sorts the data. I’d like to be able to do things:

(1) default sort (so when the user turns on the app for the first time, it would select the first segment, and sort by that action)

(2) remember where the user was between table loads. What I mean by this is, similar to Apple’s coverflow, when I go to a different cover, the UITableView repopulates. So if the last time the user was there, the sort was on the 3rd segment, then it would remember that.

I’m a bit new to object-oriented design, and this was my best guess to not have the same redundant code everywhere: (MarkersList is a NSMutableArray)

- (NSArray *)sortByName:(NSArray *)sortDescriptors {
    return [self.MarkersList sortedArrayUsingDescriptors:sortDescriptors];
}

- (NSArray *)sortByRSID:(NSArray *)sortDescriptors {
    return [self.MarkersList sortedArrayUsingDescriptors:sortDescriptors];
}

- (void)setSortedMarkersList:(NSArray *)sortedArray {
    if (self.MarkersList != nil) {
        [self.MarkersList removeAllObjects];
    }
    [self.MarkersList addObjectsFromArray:sortedArray];
}

- (IBAction)sortButtonPressed:(UISegmentedControl *)segmentControl {

    // Create sort descriptors
    NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
    NSSortDescriptor *rsID = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];

    if ([segmentControl selectedSegmentIndex] == NAME) { // this is #define 0 
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:nameDescriptor, rsIDDescriptor, nil];
        NSArray *sortedArray = [self sortByGene:sortDescriptors];
        [self setSortedMarkersList:sortedArray];
        [sortDescriptors release];

    }

    else if ([segmentControl selectedSegmentIndex] == RS_ID) {
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:rsIDDescriptor, resultDescriptor, nameDescriptor, nil];
        NSArray *sortedArray = [self sortByRSID:sortDescriptors];
        [sortDescriptors release];
        [self setSortedMarkersList:sortedArray];
    }

    [self.MarkersTableView reloadData];
}

I haven’t implemented the third sort yet since it’s not just an NSString or NSNumber like the other two yet. So far, I think it works correctly. However, the problem I have is to implement (1), I would need to call sortByName when my table is loaded. I could just create the NSSortDescriptors again, but that seems redundant. Is there a more OOD way to achieve this?

For (2), I’m guessing I could save that index for the table in a dictionary, and retrieve it when that table is loaded. Or something along those lines, not really sure.

Any help is greatly appreciated. Thanks!

  • 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-23T09:27:53+00:00Added an answer on May 23, 2026 at 9:27 am

    This isn’t so much an OO question, as iOS doesn’t always let us use best practices to solve a problem. The best bet for your situation, I think, is to store the information on the selected sort in the NSUserDefaults. In your -viewDidLoad method, check if the selected sort object exists in NSUserDefaults, use it if it does, and if not choose a reasonable default value.

    Don’t worry about creating NSSortDescriptors with each load of the application, unless you have done profiling and determined that a large amount of time is spent building it. Serializing and deserializing the NSSortDescriptors would be far more inefficient than just recreating it when needed. Apple spends a lot of time optimizing frequently used classes like NSSortDescriptor.

    Regarding #2, you can use indexPathsOfVisibleRows on the UITableView to get an array of visible indices, store the first one in UserDefaults, then on load (or pop from the next view controller if that occurs) call –scrollToRowAtIndexPath:atScrollPosition:animated:.

    Once you’re code is functioning, I recommend you watch the various videos on iTunes U regarding profiling and Instruments. It’s a wonderful tool that is often overlooked, and really helps concentrate effort where it’s needed.

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

Sidebar

Related Questions

I have a UISegmentControl that select nothing through IB, after the user selects the
I have a UITableView with custom cells that were defined in the xib file,
I have a UITableView and basically I'm making some in app settings, and if
So I have a UITableView in a .xib file. That's all that's in it.
hii every one i have created a data entry screen like this in which
I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form)
My application have UI similar to Phone.app->Recents: sectioned UITableView and a UISegmentedControl in the
I have a UISegmentControl in my app and im trying to make it switch
I have a UISegmentControl where I'd like if none of the segments are selected
I have a checklist in a UITableView and I have a UISegmentedControl with Select

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.