Reputation: 133
How to calculate number of rows for each section and populate it's data. Below is the data.
<__NSCFArray 0x7fadb3e73a60>(
{
AMID = "";
AMName = "xyz";
records = (
{
EId = 100;
EMName = "abc";
EName = "Ename1";
}
);
EGroupId = 001;
EGroupName = "SectionName1";
stat = G;
},
{
AMID = "";
AMName = "zyx";
records = (
{
EId = 144;
EMName = "namename";
EName = "somestring";
},
{
EId = 159;
EMName = "somestring";
EName = "somestring";
},
{
EId = 161;
EMName = "somestring";
EName = "somestring";
}
);
EGroupId = 86;
EGroupName = "SectionName2";
stat = Y;
}
)
From the above data, i want the "EGroupName" value in sections and dynamically count the numberofrowsinsection for each section(in this case, it's 'SectionName1 and SectionName2). Final output Should Be like,
**SectionName1**
return 1;
**SectionName2**
return 3;
really appreciate your help.
Edited
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"];
NSString *selectedEngagementId = modelItem[@"EId"];
NSLog(@"EGId %@",selectedEngagementGroupId);
NSLog(@"EId %@",selectedEngagementId);
}
Upvotes: 1
Views: 142
Reputation: 62676
The array, once converted into objects will be an array of dictionaries, where each dictionary contains an array, and each contains a string value that can be used as a section name. That's a perfectly good datasource for a multi-section table view.
After parsing the JSON, you'll have an objective c collection, call it:
NSArray *parsedObject;
The number of sections is:
parsedObject.count;
The title of a section at an indexPath is:
parsedObject[indexPath.section][@"EGroupName"]
The number of rows in a section is:
NSArray *sectionArray = parsedObject[section][@"records"];
[sectionArray count]
The model item at a given indexPath is:
NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
sectionArray[indexPath.row];
EDIT to go a little deeper, the section array itself is an array of dictionaries, so...
NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
NSDictionary *modelItem = sectionArray[indexPath.row];
NSNumber *itemId = modelItem[@"EId"];
NSNumber *itemEMName = modelItem[@"EMName"];
NSNumber *itemName = modelItem[@"EName"];
// if the table cell is a just a vanilla UITableViewCell...
cell.textLabel.text = itemName;
cell.detailTextLabel.text = itemEMName;
EDIT AGAIN Remember, all of your datasource code must follow this pattern, so to get section level attributes from the data...
NSString *engagementGroupString = engagementGroupDataArray[section][@"EGroupName"];
NSString *engagementManagerString = engagementGroupDataArray[section][@"AMName"];
EDIT AGAIN, AGAIN You may find it tiresome to type all that stuff out to get the model item each time it's needed. If so, you can compress things by creating a helper function like:
- (NSDictionary *)modelItemForIndexPath:(NSIndexPath *):indexPath {
NSArray *sectionArray = parsedObject[indexPath.section][@"records"];
return sectionArray[indexPath.row];
}
Now, for example, when the user selects something:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedEngagementGroupId = engagementGroupDataArray[indexPath.section][@"EngagementGroupId"];
NSDictionary *modelItem = [self modelItemForIndexPath:indexPath];
NSString *selectedEngagementId = modelItem[@"EId"];
NSLog(@"EGId %@",selectedEngagementGroupId);
NSLog(@"EId %@",selectedEngagementId);
}
Upvotes: 2
Reputation: 9721
I assume you know how to convert that JSON into an Objective-C collection (you will get a top-level array object I think; however you don't show the top-level object, which is not valid JSON).
You will need to create an NSMutableDictionary
property to hold your data and the key will be the section name and the values will be the associated records
array.
@property NSMutableDictionary *_sections;
However one complication is that dictionaries don't maintain order and order is very important with UITableView
, so you need to hold the order in a separate array:
@property NSMutableArray *_sectionNames;
Allocate them in the bit of code that decode the JSON and then store data in them like this:
NSArray *arrayFromJson = ...;
for (NSDictionary *dict in arrayFromJson) {
NSString *sectionName = dict[@"EGroupName"];
[_sections setObject:dict[@"records"] forKey:sectionName];
[_sectionNames addObject:sectionName];
}
If you want to sort the sections alphabetically, for example, then simply sort _sectionNames
as you like (exercise for the reader).
And then the UITableDataSource
methods will be something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *sectioName = _sectionNames[section];
return [_sections[sectionName] count];
}
Upvotes: 1