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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:59+00:00 2026-05-25T19:54:59+00:00

I have used a number of grouped tables tied to core data managed objects,

  • 0

I have used a number of grouped tables tied to core data managed objects, where the sectionNameKeyPath value is used to identify the attribute in the data that should be used to denote sections for the table.

But how do I indicate the “sectionNameKeyPath equivalent” when I have a table that is being use to present an NSMutableArray full of objects that look like this:

@interface SimGrade : NSObject {
    NSNumber * scoreValue;
    NSString * commentInfo;
    NSString * catName;
}

I would like to have sections defined according to the “catName” member of the class.

Consider, for example that my mutablearray has 5 entries where the 5 “catName” values are “Blue”, “Blue”, “Blue”, “Red”, and “Red”. So I’d want the number of sections in the table for that example to be 2.

So, what I would ‘like to do’ could be represented by the following pseudo-code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     // Return the number of sections.
    return (The number of unique catNames);
}

Note: My interest in doing this is not so much for displaying separate sections in the table, but rather so that I can more easily calculate the sums of scoreValues for each category.

<<<<< UPDATE >>>>>>>>>

Joshua’s help, as documented in his response has been right on. Here are the two new handlers for number of sections and number of rows per section…

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        NSMutableSet *counter = [[NSMutableSet alloc] init];
        [tableOfSimGrades enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
            [counter addObject:[object catName]];
        }];
        NSInteger cnt = [counter count];
        [counter release];
        NSLog(@">>>>> number of sections is -> %d", cnt);

        return cnt;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        NSMutableDictionary *counter = [[NSMutableDictionary alloc] init];
        NSMutableArray *cats = [[NSMutableArray alloc] init];
        __block NSNumber *countOfElements;
        [tableOfSimGrades enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
            // check the dictionary for they key, if it's not there we get nil                                                                                                                                                                      
            countOfElements = [counter objectForKey:[object catName]];
            if (countOfElements) {
                // NSNumbers can't do math, so we use ints.                                                                                                                                                                                           
                int curcount = [countOfElements intValue];
                curcount++;
                [counter setObject:[NSNumber numberWithInt:curcount] forKey:[object catName]];
                 NSLog(@">>>>   adding object %d to dict for cat: %@", curcount, [object catName]);

            } else {
                [counter setObject:[NSNumber numberWithInt:1] forKey:[object catName]];
                [cats addObject:[object catName]];
                NSLog(@">>>>>   adding initial object to dict for cat: %@", [object catName]);

            }

        }];

        countOfElements = [counter objectForKey:[cats objectAtIndex: section]];
        int catcount = [countOfElements intValue];

        [counter release];
        [cats release];
        return catcount;

    }

My current issue with this routine now lies in the following function… It is ignorant of any sections in the nsmutableArray and so for each section, it starts at index 0 of the array instead of at the 0th element of the appropriate section.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell =  [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...        
    SimGrade *tmpGrade = [[SimGrade alloc] init];
    tmpGrade = [tableOfSimGrades objectAtIndex: indexPath.row];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Category: %@", tmpGrade.catName];

   // [tmpGrade release];
   return cell;
}

How do I transform the “indexpath” sent to this routine into the appropriate section of the mutableArray?

Thanks,

Phil

  • 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-25T19:55:00+00:00Added an answer on May 25, 2026 at 7:55 pm

    You could do something like this:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        NSMutableSet *counter = [[NSMutableSet alloc] init];
        [arrayOfSims enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
            [counter addObject:object.catName];
        }];
        NSInteger cnt = [counter count];
        [counter release];
        return cnt;
    }
    

    you’d probably want to memoize that, for performance reasons (but only after profiling it).

    — EDIT —

    You can use an NSMutableDictionary, too, to get counts of individual categories.

       - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
       {
        NSMutableDictionary *counter = [[NSMutableDictionary alloc] init];
        __block NSNumber *countOfElements;
        [arrayOfSims enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
          // check the dictionary for they key, if it's not there we get nil                                                                                                                                                                      
          countOfElements = [counter objectForKey:object.catName];
          if (countOfElements) {
            // NSNumbers can't do math, so we use ints.                                                                                                                                                                                           
            int curcount = [countOfElements intValue];
            curcount++;
            [counter setObject:[NSNumber numberWithInt:curcount] forKey:object.catName];
          } else {
            [counter setObject:[NSNumber numberWithInt:1] forKey:object.catName];
          }
    
        }];
      NSInteger cnt = [counter count];
      // we can also get information about each category name, if we choose                                                                                                                                                                       
      [counter release];
      return cnt;
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have n number of select elements in an html page that are used
What I am looking for: I have a number of divs that are used
I have a table with a number of columns that will be used to
So here is my problem: I have used a number of interfaces in my
I have used an expression for validating a positive number as follows: ^\d*\.{0,1}\d+$ when
I have used the following code in a number of applications to load .DLL
I currently have about 650,000 items in memcached (430MB memory used) and the number
I have a number of icons used throughout an application - let's take ok/cancel
I have a number of scripts used to build a database. These need to
I have a number of enums in my application which are used as property

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.