【问题标题】:Sorting array of objects using key in dictionary使用字典中的键对对象数组进行排序
【发布时间】:2014-12-09 20:09:07
【问题描述】:

我有一个 Dict 数组,它将特定数据显示到 tableview 中,我一直在阅读如何使用属性值对对象数组进行排序,以及如何使用其中一个对 NSDictionaries 数组进行排序键,

我的问题是:-

如何在 TableView 中自定义 Section 的 header 并使用 category 作为 header section,看截图我想做同样的事情。

(
{
    categories = Protection;
    "name_en" = "Wind deflectors, front";
    "part_number" = A4221ADE00;
    "part_pic1" = "a4221ade00/a4221ade00.jpg";
},
{
    categories = Protection;
    "name_en" = "Ice & sunscreen";
    "part_number" = A4723ADE00;
    "part_pic1" = "a4723ade00/a4723ade00.jpg";
},
{
    categories = Protection;
    "name_en" = "Carens-UN-BK-2009-AccBrochure";
    "part_number" = "Carens-UN-BK-2009-AccBrochure";
    "part_pic1" = "carens-un-bk-2009-accbrochure/carens-un-2009.jpg";
},
{
    categories = Transport;
    "name_en" = "Battery Petrol engines 2.0 GDI / Diesel engine 1.7 CRDi Carens";
    "part_number" = LP370APE070CK0;
    "part_pic1" = "b2143ade00ev/b2143ade00ev-soul2014-ev-floormat.jpg";
})

【问题讨论】:

标签: ios objective-c arrays uitableview


【解决方案1】:

不确定我是否完全理解这个问题,但我会试一试。所以第一步听起来你想根据@"categories" 键对字典数组进行排序。这将根据该键按字母顺序排列数组:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"categories" ascending:YES];
NSArray *sortedArray = [arrayOfDictionaries sortedArrayUsingDescriptors:@[sortDescriptor]];

然后您希望表的部分标题是类别的名称。首先,您需要找出有多少不同的类别。像这样的方法可以将它放入一个没有重复的数组中:

+ (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries
{
    NSMutableArray *categories = [NSMutableArray array];
    for (id object in arrayOfDictionaries) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary *)object;
            if (dictionary[@"categories"] && ![categories containsObject:dictionary[@"categories"]]) {
                [categories addObject:dictionary[@"categories"]];
            }
        }
    }
    return categories;
}

此时,您有一个按类别键排序的所有字典的数组和一个包含不同类别的数组。假设上述方法中的类别数组被分配给属性self.categories(这是一个NSArray)。所以现在我们可以在正常的方法调用中填充节标题:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.categories.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return (NSString *)self.categories[section];    // I'm assuming these are all strings
}

最后,我们可以填充每个部分中的行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *headerTitle = (NSString *)self.categories[section];
    return [NameOfClass findNumberOfCategory:headerTitle inArray:self.arrayOfDictionaries];
}

+ (NSInteger)findNumberOfCategory:(NSString *)category inArray:(NSArray *)arrayOfDictionaries
{
    NSInteger number = 0;
    for (id object in arrayOfDictionaries) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary *)object;
            if (dictionary[@"categories"] && [dictionary[@"categories"] isKindOfClass:[NSString class]]) {
                NSString *thisCategory = (NSString *)dictionary[@"categories"];
                if ([thisCategory isEqualToString:category]) {
                    number++;
                }
            }
        }
    }
    return number;
}

假设self.arrayOfDictionaries 和self.categories 数组都按字母顺序排列,您可以通过从self.arrayOfDictionaries[indexPath.row] 获取对象的属性来填充tableView:cellForRowAtIndexPath: 中的单元格本身。

【讨论】:

  • numberOfRowsInSection 中类的名称是什么?
  • 另一个问题我们如何使用这个方法 + (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries
  • +开头的两个方法是类方法。因此,与实例方法(以- 开头的方法)不同,您不需要对象的实例来调用它们。阅读link 了解更多信息。假设该方法位于名为 MyViewController.m 的类中。您可以这样调用该方法:self.categories = [MyViewController findCategoriesInArray:self.arrayOfDictionaries];.
  • 感谢它的工作,抱歉我不能完全解释我的问题,但你所有的方法都在工作
  • 最后一个问题我们如何使用此方法 + (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries 我只是使用此方法过滤类别 NSArray *states = [CategoryArray valueForKey:@"categories "]; NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:states]; uniqueStates = [orderedSet 集];数组 = [uniqueStates allObjects]; NSLog(@"%@",array);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多