开发者

How to set Grouped UITableview Section names without a NSFetchedResultsController in play

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 = [[NSMutableDictiona开发者_StackOverflow社区ry 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜