Reputation: 629
I am calling some JSON and loading a table with the data from a single array. That's working great. Now I'm trying to figure out
A. the best way to load the data into the table and
B. the best way to section that data off.
This is my 6th week of iOS development and I am pretty new. I have a fairly weak Javascript background.
My first (failed attempt) way to concatenate the arrays together and pass that to the tableview. I think this is wrong for multiple reasons (issues with sectioning afterwards, know "which" one to delete, etc). Any help is greatly appreciated!
Didn't work:
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:@"all_groups"]; //NSDictionary
self.firstGroup = [self.allGroups objectForKey:@"first_group"]; //NSMutableArray
self.secondGroup = [self.allGroups objectForKey:@"second_group"]; //NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:@"third_group"]; //NSMutableArray
NSMutableArray *allGroupsArray = [self.firstGroup arrayByAddingObjectsInArray:[self.secondGroup arrayByAddingObjectsInArray:self.thirdGroup]];
Does work now, but can't figure out multiple arrays into the tableview:
-(void) getTheData {
NSString *sessionToken = [[AFOAuthCredential retrieveCredentialWithIdentifier:@"myToken"] accessToken];
if (sessionToken == nil) {
LoginViewController *loginView = [[LoginViewController alloc] init];
[self presentViewController:loginView animated:NO completion:nil];
return;
}
NSURL *url = [NSURL URLWithString:@"https://greatwebsitetogetdata.com"];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:@"MY_CLIENT" secret:@"1234567890abc"];
[oauthClient getPath:@"/api/v1/json" parameters:@{@"access_token": sessionToken} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *errorJson = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:@"all_groups"]; //This is a NSDictionary
self.firstGroup = [self.allGroups objectForKey:@"first_group"]; //This is a NSMutableArray
self.secondGroup = [self.allGroups objectForKey:@"second_group"]; //This is a NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:@"third_group"]; //This is a NSMutableArray
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.firstGroup.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *groupInfo = self.firstGroup[indexPath.row];
static NSString *cellIdentifier = @"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[@"title"];
cell.groupLikes.text= [NSString stringWithFormat:@"%@", groupInfo[@"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:@"%@ - %@", groupInfo[@"start_date"], groupInfo[@"end_date"]];
cell.groupAcceptance.text= groupInfo[@"acceptance_type"];
return cell;
}
Upvotes: 1
Views: 962
Reputation: 1201
I think an array of arrays would work better for you, where each array represents a section. allGroups should then contain 3 arrays. Then you need to override the datasource method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.allGroups.count;
}
and then in:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allGroups[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *group = self.allGroups[indexPath.section];
NSDictionary *groupInfo = group[indexPath.row];
static NSString *cellIdentifier = @"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[@"title"];
cell.groupLikes.text= [NSString stringWithFormat:@"%@", groupInfo[@"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:@"%@ - %@", groupInfo[@"start_date"], groupInfo[@"end_date"]];
cell.groupAcceptance.text= groupInfo[@"acceptance_type"];
return cell;
}
Upvotes: 2