将一个 NSMutableArray 拆分为两个,以便我可以在 UITableView 的每个部分中设置每个

Split a single NSMutableArray into two so that I can set each in each of the section in UITableView

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

例如:

if (indexPath.section==0) {

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

} else {

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

}

你可以使用这个

- (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

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

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];
        }
    }

这样使用

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]];
  }
}

尝试这种方式:-

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

}
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]];
    }
}