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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:47:02+00:00 2026-05-27T07:47:02+00:00

Let’s say that my UITableView is fed by an array of X amount of

  • 0

Let’s say that my UITableView is fed by an array of X amount of NSDates. How can I sort them so that the UITableView mimicking the Phone app on the iPhone, but in a way so that there is a section in the UITableView for each day that is represented by a date (or more than one date) in the array?

Edit: Breakthrough! To figure out how many sections I need, I use this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:@"mapSaveDataKey"];
    NSMutableArray* dateStrings = [[NSMutableArray alloc] init];
    for(NSArray* innerArray in array)
        [dateStrings addObject:[dateFormatter stringFromDate:[innerArray objectAtIndex:13]]];

    NSArray *cleanedArray = [[NSSet setWithArray:dateStrings] allObjects];

    return [cleanedArray count];
}

By taking an array of the dateStrings which are formatted NSDates using an NSDateFormatter with these settings:

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDoesRelativeDateFormatting:YES];

I can get a list of string dates, with each string from the same day being the same regardless of time. Then I can use an NSSet to get an array of the same strings excluding duplicates, and the count of that array is the number of sections I need. Great.

Now on to the hard part: how can I take that array of strings (or the original array of NSDates) and figure out how many duplicates there are of each day, and use that information to decide how many rows are needed in each section of the UITableView? And to make things more difficult, they need to be ordered from recent to old. (There is also the matter of figuring out which cells need to go in which section, but that can be figured out later)

Solved, thanks to an eloquent solution from Ole Begemann. +100 to you
I use this loop to figure out when in my array I need to start for each section:

int offset = 0;
for(int idx = 0; idx < [tableViewOutlet numberOfSections]; idx++) {
    if(idx == indexPath.section)
        break;
    offset += [tableViewOutlet numberOfRowsInSection:idx];
}
  • 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-27T07:47:03+00:00Added an answer on May 27, 2026 at 7:47 am

    Personally, I would not use date formatters. Handling strings feels somewhat “dirty” if you have more “numeric” values (dates) available. Using NSDateComponents, it is just as easy to split the date from the time components of an NSDate. The following sample code is pretty long but it should be simple to understand. Given an array of (sample) NSDate objects (in the dates variable), it generates a sections dictionary.

    The keys of the dictionary are NSDate instances that represent a certain day (their time component is mignight in the time zone of the used calendar). The values of the dictionary are arrays containing the actual dates that belong to a certain section (sorted). You should be able to populate your table view with that info.

    // Sample data: an array of dates
    NSArray *dates = [NSArray arrayWithObjects:
        [NSDate dateWithTimeIntervalSince1970:1322756697],
        [NSDate dateWithTimeIntervalSince1970:1322727896],
        [NSDate dateWithTimeIntervalSince1970:1322699096],
        [NSDate dateWithTimeIntervalSince1970:1322695496],
        [NSDate dateWithTimeIntervalSince1970:1322677496],
        [NSDate dateWithTimeIntervalSince1970:1322504696],
        nil];
    
    // First we sort the dates
    NSArray *sortedDates = [dates sortedArrayUsingSelector:@selector(compare:)];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeZone *utc = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [calendar setTimeZone:utc];
    
    // Populate the sections dictionary
    NSMutableDictionary *sections = [NSMutableDictionary dictionary];
    for (NSDate *date in sortedDates) {
        NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
        NSDate *dateRepresentingThisDay = [calendar dateFromComponents:dateComps];
        NSMutableArray *datesOnThisDay = [sections objectForKey:dateRepresentingThisDay];
        if (datesOnThisDay == nil) {
            // This day is new.
            // Create an empty array and add it to the dictionary under this day as key.
            datesOnThisDay = [NSMutableArray array];
            [sections setObject:datesOnThisDay forKey:dateRepresentingThisDay];
        }
        [datesOnThisDay addObject:date];
    }
    
    // Output the result    
    NSLog(@"Found %u sections.", [sections count]);
    NSArray *sortedDays = [[sections allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSDate *day in sortedDays) {
        NSLog(@"Section: %@", day);
        NSArray *datesOnThisDay = [sections objectForKey:day];
        for (NSDate *date in datesOnThisDay) {
            NSLog(@"-- Entry: %@", date);
        }
    }
    

    Edit: I took this question as an opportunity to write a blog post about this problem.

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

Sidebar

Related Questions

Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I can call a method like this: core::get() . What is the
Let's say I have some text as follows: do this, do that, then this,
Let's say I have deployed my Rails app on a VPS, and everything works
Let's say, I have a .NET 2 installed. Can I programmatically install version 4
Let's say I create an object like this: Person: NSString *name; NSString *phone; NSString
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say that I have a set of relations that looks like this: relations
Let's say I have a javascript array with a bunch of elements (anywhere from

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.