Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set Grouped UITableview Section names without a NSFetchedResultsController in play
    primarykey
    data
    text
    <p>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. </p> <p>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:</p> <pre><code>@interface SimGrade : NSObject { NSNumber * scoreValue; NSString * commentInfo; NSString * catName; } </code></pre> <p>I would like to have sections defined according to the "catName" member of the class.</p> <p>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.</p> <p>So, what I would 'like to do' could be represented by the following pseudo-code:</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return (The number of unique catNames); } </code></pre> <p>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.</p> <p>&lt;&lt;&lt;&lt;&lt; UPDATE >>>>>>>>></p> <p>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...</p> <pre><code> - (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(@"&gt;&gt;&gt;&gt;&gt; number of sections is -&gt; %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(@"&gt;&gt;&gt;&gt; adding object %d to dict for cat: %@", curcount, [object catName]); } else { [counter setObject:[NSNumber numberWithInt:1] forKey:[object catName]]; [cats addObject:[object catName]]; NSLog(@"&gt;&gt;&gt;&gt;&gt; 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; } </code></pre> <p>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.</p> <pre><code>- (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; } </code></pre> <p>How do I transform the "indexpath" sent to this routine into the appropriate section of the mutableArray?</p> <p>Thanks,</p> <p>Phil</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload