2

我需要从下面的数据中显示一个分组的表格视图。我需要根据"account_type"对下面的数组进行分类。

例如:我需要显示表部分标题“储蓄”并列出所有储蓄类型账户,然后同样获得唯一账户类型并将其作为表行中的部分标题和帐号。我可以使用 NSSet 获取节标题,但是如何获取行数并将其显示在 UITableView 中。

<__NSArrayM 0x7f8ef1e8b790>(
{
"account_no" = 123;
"account_type" = Savings;
},
{
"account_no" = 123456;
"account_type" = Savings;
},
{
"account_no" = 00000316;
"account_type" = "DPN STAFF NON EMI";
},
{
"account_no" = 1000000552;
"account_type" = "DPN STAFF EMI LOANS";
})

我需要在 UITableView 中显示上述数据,例如

第 0 节 --- 储蓄

第 1 - 123 行

第 2 行 - 123456

第 1 节 ---> DPN 员工非 EMI

第 1 行 - 00000316

谢谢,

AKC

4

2 回答 2

2

试试下面的代码:

NSMutableArray *resultArray = [NSMutableArray new];
    NSArray *groups = [arrySelectedAcctDetails valueForKeyPath:@"@distinctUnionOfObjects.account_type"];

NSLog(@"%@", groups);

for (NSString *groupId in groups)
{
    NSMutableDictionary *entry = [NSMutableDictionary new];
    [entry setObject:groupId forKey:@"account_type"];

    NSArray *groupNames = [arrySelectedAcctDetails filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"account_type = %@", groupId]];

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

 NSLog(@"%@", resultArray);

输出:

{
        "account_no1" = 00000316;
        "account_type" = "DPN STAFF  NON EMI";
    },
        {
        "account_no1" = 123;
        "account_no2" = 123456;
        "account_type" = Savings;
    },
于 2016-03-09T06:23:16.893 回答
2

您也可以使用 NSDictionary。下面的代码完美运行。

if([arrySelectedDetails count] >0){

        grouped = [[NSMutableDictionary alloc] initWithCapacity:arrySelectedAcctDetails.count];
        for (NSDictionary *dict in arrySelectedDetails) {
            id key = [dict valueForKey:@"type"];

            NSMutableArray *tmp = [grouped objectForKey:key];
            if (tmp == nil) {
                tmp = [[NSMutableArray alloc] init];
                [grouped setObject:tmp forKey:key];

            }
            [tmp addObject:dict];

        }


        typeArray= [[NSMutableArray alloc]init];

        for(NSDictionary *groupId in arrySelectedDetails){

            if(!([typeArray count]>0)){
                [typeArray addObject:[groupId valueForKey:@"type"]];

            }
            else if (![typeArray containsObject:[groupId valueForKey:@"type"]]) {
                [typeArray addObject:[groupId valueForKey:@"type"]];

            }

        }
    }

然后对于 UITableView 代表:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [typeArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [grouped[[typeArray objectAtIndex:section]] count]
}
于 2016-03-18T05:42:41.860 回答