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

时间:2016-04-25 06:24:23

标签: ios objective-c iphone uitableview nsmutablearray

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

例如:

if (indexPath.section==0) {

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

} else {

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

}

5 个答案:

答案 0 :(得分: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];
        }
    }

答案 1 :(得分:0)

您可以使用此

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

答案 2 :(得分:0)

像这样使用

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

答案 3 :(得分:0)

尝试这种方式: -

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

}

答案 4 :(得分:0)

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