NSMutableArray 在 UITableView 中排序显示
NSMutableArray sorting and displaying in UITableView
我需要显示来自以下数据的分组 table视图。我需要根据 "account_type" 对以下数组进行分类。
例如:我需要显示 Table 部分标题 "Savings" 并列出所有储蓄类型账户,然后类似地获取唯一账户类型并将其作为部分 header 和帐号在 table 行中。我可以使用 NSSet 获取 headers 部分,但是如何获取行数并将其显示在 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";
})
我需要在UITable中显示以上数据
第 0 部分 --- 储蓄
第 1 - 123 行
第 2 行 - 123456
第 1 部分 ---> DPN 员工非 EMI
第 1 行 - 00000316
谢谢,
AKC
试试下面的代码:
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;
},
您也可以使用 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]
}
我需要显示来自以下数据的分组 table视图。我需要根据 "account_type" 对以下数组进行分类。
例如:我需要显示 Table 部分标题 "Savings" 并列出所有储蓄类型账户,然后类似地获取唯一账户类型并将其作为部分 header 和帐号在 table 行中。我可以使用 NSSet 获取 headers 部分,但是如何获取行数并将其显示在 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";
})
我需要在UITable中显示以上数据
第 0 部分 --- 储蓄
第 1 - 123 行
第 2 行 - 123456
第 1 部分 ---> DPN 员工非 EMI
第 1 行 - 00000316
谢谢,
AKC
试试下面的代码:
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;
},
您也可以使用 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]
}