【问题标题】:iPhone - Help with adding 9 different sections inside a UITableViewiPhone - 帮助在 UITableView 中添加 9 个不同的部分
【发布时间】:2011-03-12 11:55:12
【问题描述】:

我有一个NSArray 的对象:

MyObject 包含以下字符串属性:

Title
Category
Name

共有9个类别。

我想在UITableView 中显示这些类别。

目前我的代码看起来像这样,但它并不完全正确,我不确定如何处理我的各个类别

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;` as well.

    for (ArrayOfMyObject *item in myArray) {
      if ([item.Category isEqualToString:@"Category1"]) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjects:item forKey:item.Category];
        [self.itemsArray addObject:dict];
      }

      if ([item.Category isEqualToString:@"Category2"]) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjects:item forKey:item.Category];
        [self.itemsArray addObject:dict];
      }
      ... (etc, for the rest of the Category titles).
    }

如果有 9 个类别,我应该得到总共 9 个部分,但看起来我实际上得到了项目的总数(大约 76 个)。任何帮助将不胜感激,如果需要,请随时请求更多代码。

我想我还需要知道如何在 cellForRowAtIndexPath 方法中处理这个问题。

我会做这样的事情吗?

MyObject *item = (MyObject *)[self.itemsArray objectAtIndex:indexPath.row];
NSMutableDictionary *dictionary = [self.itemsArray objectAtIndex:indexPath.section];
NSArray *categoryArray = [dictionary objectForKey:item.Category];

【问题讨论】:

    标签: iphone objective-c nsmutablearray nsdictionary nsmutabledictionary


    【解决方案1】:

    我将从以不同的方式组织您的数据源开始。

    获取一个数据源 NSArray,其中包含 9 个代表您的类别的 NSDictionaries。在每个类别中,您有几个键 - 一个用于具有类别名称的相应字符串,一个用于属于该类别的相应 MyObjects 数组:

    dataSource [
                 item1 {
                         catName = @"CategoryName 1"
                         objects = NSArray of MyObjects for catName1
                       }
                 item2 {
                         catName = @"CategoryName 2"
                         objects = NSArray of MyObjects for catName2
                       }
                 etc...
               ]
    

    以这种方式组织数据后,表格视图的数据源方法将如下所示(概念上):

    numberOfSections 可以通过[datasource count] 访问

    titleForSection 可以通过[[datasource objectAtIndex:indexPath.section] valueForKey:@"catName"]访问

    numberOfRowsInSection 可以通过[[[datasource objectAtIndex:indexPath.section] valueForKey:@"objects"] count] 访问

    最后,可以使用 cellForRowAtIndexPath: 方法上的[[[datasource objectAtIndex:indexPath.section] valueForKey:@"objects"] objectAtIndex:indexPath.row] 访问每一行的 MyObject。

    有意义吗? 如果您有任何问题,请告诉我。 干杯, 罗格

    [编辑以回答您的后续问题]

    这是基于一些假设,但您应该了解它的要点。为了测试这一点,我创建了一个 MyObject 类,它返回一个带有随机生成的类别名称的实例。

    - (void)createDatasource
    {
        NSInteger numberOfObjects = 10;
        NSMutableArray *objects = [NSMutableArray arrayWithCapacity:numberOfObjects];
        NSMutableArray *categories = [NSMutableArray arrayWithCapacity:numberOfObjects];
        for (int i = 0; i < numberOfObjects; i++)
        {
            MyObject *obj = [[MyObject alloc] init];
            [objects addObject:obj];
            [categories addObject:obj.category];
            [obj release];
        }
        NSSet *set = [NSSet setWithArray:categories];
        NSMutableArray *dataSource = [[NSMutableArray alloc] initWithCapacity:[set count]];
        for (NSString *categoryString in set)
        {
            NSMutableDictionary *mainItem = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil, @"categoryName", nil, @"myObjects", nil];
            NSMutableArray *mainItemMyObjects = [NSMutableArray array];
            [mainItem setValue:categoryString forKey:@"categoryName"];
            for (MyObject *obj in objects)
            {
                if ([obj.category isEqualToString:categoryString])
                {
                    [mainItemMyObjects addObject:obj];
                }
            }
            [mainItem setValue:mainItemMyObjects forKey:@"myObjects"];
            [dataSource addObject:mainItem];
            [mainItem release];
        }
        NSLog (@"objects = %@\ncategories = %@\nset = %@\ndatasource = %@", objects, categories, set, dataSource);
        // Use and release your datasource as appropriate
        // You may want to sort your datasource appropriately depending on your needs
    }
    

    【讨论】:

    • 有道理,但你能详细说明一下数据源的创建吗?
    • @Fulvio 我做了一些假设 - 请参阅编辑后的帖子。还有一种替代方法是使用 NSFetchedResultsController 但如果您的数据不会一直更改(并且必须相应地刷新表),上述结果应该会达到类似的效果。如果是这种情况,请查看获取的结果控制器文档以了解详细信息。
    • 我有一个日期,它是 MyObject 的一个属性。理想情况下,我希望能够按日期降序对数据进行排序。因此,因此将首先显示最新的项目(因此显然根据第一个项目/部分是什么,每次都以不同的顺序显示这些部分)。非常感谢您当前的回答,我肯定会使用这个并接受这个作为答案,但我只是想知道您是否也可以帮我订购?谢谢。
    • @Fulvio 不用担心 - 这里有一个很好的参考资料可以帮助您进行排序stackoverflow.com/questions/805547/…
    • 我看到了那个参考,试图以我认为可行的方式实现它,但是我只能对每个部分中的数据进行排序。我所追求的是按照数据的顺序对这些部分进行排序——如果这有意义的话。
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2023-03-06
    • 2016-06-10
    • 2011-12-17
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多