【问题标题】:How to get looping item count in collectionView numberOfItemsInSection如何在collectionView numberOfItemsInSection中获取循环项目数
【发布时间】:2018-04-10 03:16:17
【问题描述】:

我有一个NSMutableArray,如下:-

在我的UICollectionView numberOfItemsInSection 中,我如何获取attribute_name 的项目数?例如,在 attribute_group_name Color section 下,我会得到 4 个项目。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.attributeGroupList[section].count;  //HOW CAN I GET ITEM COUNT FOR ATTRIBUTE_NAME?
}

更新:-

具有新数据结构的示例输出:-

 {
        "attribute_group_name" = Color;
        names =         (
            Black,
            White,
            Gold,
            Red
        );
    },
        {
        "attribute_group_name" = Service;
        names =         (
            "Free Shipping",
            Insurance
        );
    },
        {
        "attribute_group_name" = Telco;
        names =         (
            Digi,
            Maxis,
            Celcom
        );
    },
        {
        "attribute_group_name" = Material;
        names =         (
            Wood
        );
    },
        {
        "attribute_group_name" = Brand;
        names =         (
            Apple,
            Mi,
            Gome,
            HuaWei
        );
    },
        {
        "attribute_group_name" = RAM;
        names =         (
            4G,
            8G,
            16G
        );
    },
        {
        "attribute_group_name" = Size;
        names =         (
            1200sqf,
            908sqf
        );
    },
        {
        "attribute_group_name" = Location;
        names =         (
            "Petaling Jaya",
            "Kuala Lumpur",
            Ipoh
        );
    },
        {
        "attribute_group_name" = Memory;
        names =         (
            32G,
            64G,
            128G,
            256G
        );
    },
        {
        "attribute_group_name" = Warranty;
        names =         (
            "1 Year"
        );
    }

【问题讨论】:

  • 你的字典设计得很糟糕。而不是拥有可变键,您应该有一个键,例如attributes,它是Strings 或其他的数组。也请发布文字,而不是图片
  • 您好 Paulw11,将注意帖子文本而不是图像。谢谢!

标签: ios objective-c uicollectionview


【解决方案1】:

如果我查看您的数据结构,我会说:

self.attributeGroupList[section].count-1

“-1”在这里是因为看起来您的数据结构只有一个不同的键。 但我觉得这不是你等待的答案。

这里真正的问题是你的数据结构,我肯定会这样做:

{
 "group_name": Color;
 "names":["Black","White","Red"];
}

这样你就可以做这样的事情:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.attributeGroupList[section][@"names"].count;
}

编辑:

好的,所以你做得很好,唯一需要改变的是那部分:

    for (int i = 0; i < groupNames.count; i++)
    {
        NSString *name = [[groupNames objectAtIndex:i] objectForKey:@"attribute_name"];
        [entry setObject:name forKey:[NSString stringWithFormat:@"attribute_name%d", i + 1]];
    }

只需要那样做,我相信有更聪明的方法可以做到,但这里很简单:

NSMutableArray *resultArray = [NSMutableArray new];
NSArray *groups = [_filterItem valueForKeyPath:@"@distinctUnionOfObjects.attribute_group_name"];
for (NSString *attribute_group_name_header in groups){
     NSMutableDictionary *entry = [NSMutableDictionary new];
     [entry setObject:attribute_group_name_header forKey:@"attribute_group_name"];

      NSArray *groupNames = [_filterItem filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"attribute_group_name = %@", attribute_group_name_header]];
      NSMutableArray *names = [NSMutableArray new];
      for (int i = 0; i < groupNames.count; i++){
           NSString *name = [[groupNames objectAtIndex:i] objectForKey:@"attribute_name"];
           [names addObject: name];
      }
      [entry setObject: names forKey:@"names"];
      [resultArray addObject:entry];
}

编辑 2:

查看您的最终对象后,我似乎忘记在“names”字符串之前添加“@”。因此,请确保在任何字符串之前都有它(第一个 EDIT 已相应更正)。

另外,也许在添加到数组names 之前添加一个这样的字符串转换:(NSString *),就像这样:

 NSString *name = (NSString *)[[groupNames objectAtIndex:i] objectForKey:@"attribute_name"];

这会给你一个字符串数组,而不是一个随机对象数组(就像你现在拥有的那样),这可能很难使用。

编辑 3:

我们可以做到啊。 看起来它没有抓住关键的“名字” 所以你需要更具体。类似的东西:

return ((NSArray *)[self.attributeGroupList[0] objectForKey:@"names"]).count

请记住,这段代码非常糟糕。我正在将一个对象强制转换为一个数组。这是一个假设,您应该在此之前对对象进行一些测试。因为如果你的数据结构发生变化,这就会崩溃。

注意:

您针对一个问题提出了很多问题。 Stackoverflow 旨在解决一个精确且定义明确的问题,以便您的问题对其他人有所帮助。一般来说,在这里你肯定需要关于你的代码的帮助。所以这里绝对不是发这个的地方。我建议你看看像 Codementor 这样的网站

【讨论】:

  • 嗨 RomOne,我可以知道如何设置数据结构,如您提出的解决方案吗?
  • 当然。你从哪里得到这些数据?是您在应用中设置的内容,还是您从 API 获得的内容?
  • 嗨 RomOne,请查看我更新的问题。这就是我做的糟糕的结构。
  • 抱歉,我觉得我点击按钮太快了,我可能错误地编辑了您的帖子。我无法看到您的更改,您可以将其添加回来吗?谢谢:)
  • 请尝试重新审核。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2016-09-25
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
相关资源
最近更新 更多