我正在调用一些JSON并使用单个数组中的数据加载表。那工作得很好。现在我想弄清楚
A。将数据加载到表格中的最佳方式
B。关闭数据的最佳方法。
这是iOS开发的第6周,我很新。我有一个相当弱的Javascript背景。
我的第一个(尝试失败)方法将数组连接在一起并将其传递给tableview。我认为这是错误的,原因有很多(后来切片的问题,知道“删除哪一个”等)。非常感谢任何帮助!
无效:
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]];
现在可以正常工作,但无法在表格视图中找出多个数组:
-(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;
}
答案 0 :(得分:2)
我认为数组数组对你来说会更好,每个数组代表一个部分。然后allGroups应该包含3个数组。 然后你需要覆盖数据源方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.allGroups.count;
}
然后在:
- (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;
}