ios - 如何将 NSMutableDictionary setObject 的值赋值成为 indexPath.section 的值?

标签 ios objective-c uicollectionview nsdictionary

我有 NSMutableDictionary 并返回结果如下:-

NSMutableDictionary *result = [NSMutableDictionary new];
        distinctNames = [_aryClippedCategoryList valueForKeyPath:@"@distinctUnionOfObjects.category_id"];
        for (NSString *category_id in distinctNames) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category_id = %@", category_id];
            NSArray *category_group = [_aryClippedCategoryList filteredArrayUsingPredicate:predicate];
            [result setObject:category_group forKey:category_id];

NSLog(@"123 - %@", result); 
NSLog(@"223 - %lu", (unsigned long)distinctNames.count); 

在 NSLog 123 中,结果将如下所示:-

 17 =     (
                {
            "category_id" = 17;
            "category_name" = "Apparel & Accessories";
            "sub_category_id" = 17;
            "sub_category_name" = "Fashion jewelry";
        },
                {
            "category_id" = 17;
            "category_name" = "Apparel & Accessories";
            "sub_category_id" = 16;
            "sub_category_name" = Wholesale;
        }   
    );
    20 =     (
                {
            "category_id" = 20;
            "category_name" = "Beauty & Personal Care";
             "sub_category_id" = 288;
            "sub_category_name" = "Pedicure & medicure";
        },
                {
            "category_id" = 20;
            "category_name" = "Beauty & Personal Care";
            "sub_category_id" = 285;
            "sub_category_name" = "Fragrances and perfumes";
        }
);

在 NSLog 223 中,结果将如下所示:-

223 - 2

在这种情况下,如何在 CollectionView 中选择 category_group 作为部分值并在 CollectionView 单元格中显示每组项目(category_id、category_name、sub_category_id、sub_category_name)?

最佳答案

  • (不推荐)您应该使用 NSMutableArray 而不是 NSMutableDictionary。

    结构是

    Array<Dictionary<Array *> *> *
    

    代码是这样的

    NSMutableArray *groups = [NSMutableArray new];
    <# your code #>
    [groups addObject:@{
        @"category_id": category_id,
        @"infos": category_group
    }];
    

    然后

    NSString *groupID = groups[indexPath.section][@"category_id"]
    

    这是您需要的部分的组值。

    NSArray *infos = groups[indexPath.section][@"infos"];
    NSDictionary *info = infos[indexPath.item];
    

    这是对象dict元素值(value)

  • (推荐)或改变您的dataobject . (对象NSMutableArray<Object *> *)

    然后使用groups.count获取部分。

    groups[indexPath.section].categoryID
    

    这是您需要的 sections 的组值.

    group = groups[indexPath.section]
    object = group.infos[indexPath.item]
    

    这是 items 的对象值

关于ios - 如何将 NSMutableDictionary setObject 的值赋值成为 indexPath.section 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533361/

相关文章:

ios - 如何设计 ARKit 使用的 Animoji 3D 模型?

ios - 关于 UIButton 的 titleLabel 和 imageView 属性的困惑

iphone - 使用 remoteIO 生成 iPhone 音频时如何防止爆音?

ios - 在 sizeForItemAtIndexPath 中调用 UICollectionView Cell

cocoa-touch - (Xcode) UIButtons 和页面之间的链接

ios - 展开可选时为零值

ios - UICollectionView.reloadData() 更改单元格顺序 | iOS swift

ios - Pinterest 风格布局 iOS Storyboard

ios - 显示 map 的 Sygic 问题

iphone - 使用 scandit sdk 的条码读取器