2

I need to show a grouped tableview from the below data. I need to categorise the below array based on "account_type".

For Eg: I need to show Table Section Heading "Savings" and list all savings type accounts, then similarly get Unique account types and gave that as section header and account numbers in table rows. I am able to get section headers using NSSet, but how to get row counts and display it in a 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";
})

I need to display the above data in UITableView like

section 0 --- Savings

Row 1 - 123

Row 2 - 123456

section 1 ---> DPN STAFF NON EMI

Row 1 - 00000316

Thanks,

AKC

Mobile Developer
  • 231
  • 2
  • 15

2 Answers2

2

Try the below code:

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);

Output:

{
        "account_no1" = 00000316;
        "account_type" = "DPN STAFF  NON EMI";
    },
        {
        "account_no1" = 123;
        "account_no2" = 123456;
        "account_type" = Savings;
    },
2

You can make use of NSDictionary also. The below code worked perfectly.

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

            }

        }
    }

Then for UITableView Delegates:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [grouped[[typeArray objectAtIndex:section]] count]
}