【问题标题】:Split a single NSMutableArray into two so that I can set each in each of the section in UITableView将单个 NSMutableArray 拆分为两个,以便我可以在 UITableView 的每个部分中设置每个
【发布时间】:2016-08-18 10:01:23
【问题描述】:

我想使用多个部分功能UITableView。我有一个NSMutableArray,其中包含字典格式的数据。在该字典中有一个键,其值为“0”和“1”。 我想从中创建两个单独的NSMutableArray,以便我可以在不同的部分中相应地分配它们。

例如:

if (indexPath.section==0) {

NSDictionary *Data = [roomList1 objectAtIndex:indexPath.row];

} else {

NSDictionary *Data = [roomList2 objectAtIndex:indexPath.row];

}

【问题讨论】:

  • 字典中值 0/1 的键是什么。您可以使用谓词来获取具有 0 和 1 的不同值数组
  • 你能在这里给我看一些json数据吗?
  • LIST:( { d = "1"; g = "Local"; id = 9550; n = testapp1; s = available; }, { d = "0" g = "Local"; id = 5319;n = chatplus1;s = away;}, })
  • 需要根据那个'd'键拆分它

标签: ios objective-c iphone uitableview nsmutablearray


【解决方案1】:

假设您的字典中的值始终设置,您可以执行以下操作:

NSMutableArray *firstArray = [NSMutableArray new];
NSMutableArray *secondArray = [NSMutableArray new];

for (NSDictionary *dictionary in yourArray) {
        if ([dictionary objectForKey:@"yourValue"] isEqualToString: @"0") {
            [firstArray addObject:dictionary];
        } else if ([dictionary objectForKey:@"yourValue"]isEqualToString: @"1") {
             [secondArray addObject:dictionary];
        }
    }

【讨论】:

  • 然后不要将其转换为整数并使用 isEqualToString: @0" 或 @"1" 检查 Madhumitha 至少回答比较部分;)
  • 我是 ios 的新手,你能解释一下“// 将字典添加到第一个数组数据”
  • 这只是一个评论,应该解释你需要做什么,但我为你添加了我的答案。
【解决方案2】:

你可以用这个

- (NSDictionary *)groupObjectsInArray:(NSArray *)array byKey:(id <NSCopying> (^)(id item))keyForItemBlock
    {
        NSMutableDictionary *groupedItems = [NSMutableDictionary new];
        for (id item in array) {
            id <NSCopying> key = keyForItemBlock(item);
            NSParameterAssert(key);

            NSMutableArray *arrayForKey = groupedItems[key];
            if (arrayForKey == nil) {
                arrayForKey = [NSMutableArray new];
                groupedItems[key] = arrayForKey;
            }
            [arrayForKey addObject:item];
        }

        return groupedItems;
    }

参考:Split NSArray into sub-arrays based on NSDictionary key values

【讨论】:

    【解决方案3】:

    这样使用

    NSMutableArray * roomList1 = [[NSMutableArray alloc] init];
    NSMutableArray * roomList2 = [[NSMutableArray alloc] init];
    
    for(int i = 0;i< YourWholeArray.count;i++)
    {
      if([[[YourWholeArray objectAtIndex:i] valueForKey:@"YourKeyValueFor0or1"] isEqualToString "0"])
      { 
         [roomList1 addObject:[YourWholeArray objectAtIndex:i]];
      }
      else if([[YourWholeArray objectAtIndex:i] valueForKey:@"YourKeyValueFor0or1"] isEqualToString "1"])
      {
         [roomList2 addObject:[YourWholeArray objectAtIndex:i]];
      }
    }
    

    【讨论】:

    • 比较字符串时我会使用 isEqualToString:。
    • 但这会终止应用程序并出现以下错误:*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI isEqualToString:]:无法识别的选择器已发送到实例 0x78e0b210”跨度>
    • 这是我在主要 NSMutabledictionary LIST 上的列表:( { d = "1"; g = " Local"; id = 9550; n = testapp1; s = available; }, { d = "0" g = "本地"; id = 5319; n = chatplus1; s = away; }, })
    • 使用您提供的 sn-p 处理此数据后,以下是响应中出现的错误:2016-04-25 12:23:34.916 sampleproj[2519:39481] -[__NSArrayI isEqualToString:]: unrecognized选择器发送到实例 0x7b622000 2016-04-25 12:23:35.091 sampleproj[2519:39481] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSArrayI isEqualToString:]:无法识别的选择器发送到实例 0x7b622000 ' *** 首先抛出调用堆栈:
    【解决方案4】:
    NSMutableArray *roomList1 = [[NSMutableArray alloc] init];
    NSMutableArray *roomList2 = [[NSMutableArray alloc] init];
    
    for (NSString *key in [myDictionary allKeys]) {
        if (myDictionary[key] isEqualToString: @"0") {
            [roomList1 addObject: myDictionary[key]];
        } else {
            [roomList2 addObject: myDictionary[key]];
        }
    }
    

    【讨论】:

      【解决方案5】:

      试试这个方法:-

      NSMutableArray *getdata=[[NSMutableArray alloc]init];
      getdata=[results objectForKey:@"0"];
      
      NSMutableArray *getdata2=[[NSMutableArray alloc]init];
      getdata2=[results objectForKey:@"1"];
      
      use this two array and populate the section with now of rows at indexpathrow
      
      define number of section 2
      
      if (section==0){
      
      getdata
      
      }
      else{
      
      getdata2
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-06
        • 2016-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-07
        相关资源
        最近更新 更多