如何在没有 NSFetchedResultsController 的情况下设置分组 UITableview 部分名称

发布于 2024-12-07 02:59:57 字数 4037 浏览 0 评论 0原文

我使用了许多与核心数据管理对象相关的分组表,其中sectionNameKeyPath 值用于标识数据中应用于表示表的部分的属性。

但是,当我有一个表用于呈现一个充满如下所示对象的 NSMutableArray 时,如何指示“sectionNameKeyPath 等效项”:

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

我希望根据类的“catName”成员定义节。

例如,考虑一下我的可变数组有 5 个条目,其中 5 个“catName”值为“Blue”、“Blue”、“Blue”、“Red”和“Red”。因此,我希望该示例的表中的节数为 2。

因此,我“想要做的”可以用以下伪代码表示:

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

注意:我对此的兴趣并不大用于在表中显示单独的部分,而是为了让我可以更轻松地计算每个类别的 ScoreValues 总和。

<<<<<<<更新>>>>

正如约书亚的回应中所记录的那样,他的帮助是正确的。这是用于部分数和每部分行数的两个新处理程序...

   - (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;

    }

我当前的例程问题在于以下函数...它不知道 nsmutableArray 中的任何部分,因此对于每个部分,它从数组的索引 0 开始,而不是从相应部分的第 0 个元素开始。

- (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;
}

如何将发送到此例程的“indexpath”转换为 mutableArray 的适当部分?

谢谢,

菲尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-12-14 02:59:57

您可以这样做:

- (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;
}

出于性能原因,您可能想要记住这一点(但只有在对其进行分析之后)。

--- 编辑 ---

您也可以使用 NSMutableDictionary 来获取各个类别的计数。

   - (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;
}  

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;
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文