我已经成功地从 API 解析 JSON 并将其显示在 tableview 中。我收到的数据是他注册的用户的课程,但是我收到的数据只是班级 ID,所以我必须获取每个班级 ID 并发送单独的 API 请求以获取班级名称和教师。我可以在单独的视图控制器上成功执行此操作,因为如果用户单击该行,我可以获得班级 ID,但是,我想在单个表格视图上显示班级名称、教师和 ID。
所以我的问题是,我将如何从我的表格视图中获取数据并为每个单元格发出单独的 api 请求并将数据显示到该单元格上?
我目前的数据:
如果我单击一行,我会得到以下数据:
2013-06-17 17:06:59.232 App[22024:c07] Class: Honors Pre-Calculus
2013-06-17 17:06:59.233 App[22024:c07] Teacher: Joe Shmo
如何在一个屏幕上同时显示两者?
这是我在第一个屏幕上检索类 ID 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([searchResults count] == 0) {
return [tableView dequeueReusableCellWithIdentifier:
NothingFoundCellIdentifier];
} else {
ScheduleResultCell *cell = (ScheduleResultCell *)[tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];
ScheduleResult *searchResult = [searchResults objectAtIndex:indexPath.row];
NSString *classFormat = @"%@";
NSString *classfks = searchResult.class_fk;
NSString *classfk = [NSString stringWithFormat:classFormat, classfks];
cell.idLabel.text = classfk;
return cell;
}
}
这是我在第二个屏幕上检索课程信息的代码:
-(void)viewDidAppear:(BOOL)animated {
url = [NSString stringWithFormat:@"https://myapiurl.com/v1/classes/%@.json", idValue];
//show loader view
[SVProgressHUD showWithStatus:@"Loading Info"];
//fetch the feed
_feed = [[ClassFeed alloc] initFromURLWithString:url
completion:^(JSONModel *model, JSONModelError *err) {
//hide the loader view
[SVProgressHUD dismiss];
//json fetched
NSLog(@"Class: %@", _feed.course);
NSLog(@"Teacher: %@", _feed.teacher_full_name);
NSLog(@"Meeting Info: %@", _feed.meeting_times);
[self.tableView reloadData];
}];
}
知道如何在一个屏幕上完成此操作吗?
这是我一直在尝试做的事情,但它似乎只在一个单元格上显示数据:
int count = [searchResults count];
NSString *classFormat = @"%@";
NSString *classfks = searchResult.class_fk;
NSString *classfk = [NSString stringWithFormat:classFormat, classfks];
for (int i=0; i < count; i++) {
ScheduleResult *classInfo = [searchResults objectAtIndex:indexPath.row];
NSString *classId = classInfo.class_fk;
NSLog(@"i value: %d", i);
url2 = [NSString stringWithFormat:@"https://apiurl.com/v1/classes/%@.json", classId];
NSLog(@"url value: %@", url2);
[SVProgressHUD showWithStatus:@"Loading Info"];
_feed = [[ClassFeed alloc] initFromURLWithString:url2
completion:^(JSONModel *model, JSONModelError *err) {
//hide the loader view
[SVProgressHUD dismiss];
//json fetched
NSLog(@"Class #%d: %@", i, _feed.course);
NSLog(@"Teacher #%d: %@", i, _feed.teacher_full_name);
NSLog(@"Meeting #%d: %@", i, _feed.meeting_times);
//name = namefk;
NSString *nameFormat = @"%@";
NSString *namefks = _feed.course;
NSString *namefk = [NSString stringWithFormat:nameFormat, namefks];
cell.nameLabel.text = namefk;
//[self.tableView reloadData];
}];
}
我尝试使用不同的方法将数据添加到数组中,但我什至无法正确完成。
NSMutableArray* fullArray;
fullArray = [[NSMutableArray alloc] init];
[fullArray addObject:classfk];
NSLog(@"array: %@", fullArray);
调试输出:
array: ( 10326
) 2013-06-17 23:36:45.488 App[30088:c07] array: ( 10371 )
2013-06-17 23:36:45.494 App[30088:c07] array: (
10420 )
如果这个问题没有多大意义,我想问一下如何从这个 JSON 提要(第一个 url)中获取 class_fk:
[{
"class_fk": 10326,
"currently_enrolled": true,
"date_withdrawn": null,
"enrollment_pk": 147745,
"fee_paid": false,
"late_date_enrolled": null,
"level": null,
"student_fk": 132664,
"update_date": "2012-08-27"
},
{
"class_fk": 10371,
"currently_enrolled": true,
"date_withdrawn": null,
"enrollment_pk": 147168,
"fee_paid": false,
"late_date_enrolled": null,
"level": null,
"student_fk": 132664,
"update_date": "2012-08-23"
}
并使用它插入另一个 url 并从中获取课程和teacher_full_name?(第二个网址):
{
"class_id": "ADVMTG-40",
"class_pk": 10326,
"course": "Advisor Meeting",
"course_id": 7,
"course_type": "Advisory",
"description": "ADV: Shmo, J.",
"group_fk": 13980,
"primary_grade_level": "None",
"school_level": "Upper School",
"school_year": 2012,
"status": "Active",
"subject": "Administration",
"teacher_fk": 80404,
"teacher_full_name": "Joe Shmo",
"update_date": "2012-10-23T10:06:00-05:00",
"teachers": [
{
"person_fk": 80404,
"role": "Primary Teacher",
"role_id": 1,
"teacher_name": "Joe Shmo",
"update_grades": true,
"view_grades": true
}
],
"meeting_times": [
{
"block": "Advisor Meetings",
"block_abbreviation": "ADV",
"day": "Friday",
"end_time": null,
"grading_period": "ALL",
"room": null,
"start_time": null
}
]}
这是我的问题的解决方案(非常感谢您的帮助!):
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//[searchResults sortUsingSelector:@selector(compareName:)];
NSString *urlString = @"https://apiurl.com/v1/enrollments.json?student=132664";
NSURL *Url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:Url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *classes = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
//NSLog(@"classes array: %@", classes);
//now we loop through all classes
for (NSMutableDictionary *class in classes) {
//we get individual data for each class
NSString *classID = class[@"class_fk"];
//NSLog(@"classID: %@", classID);
NSString *classUrlString = [NSString stringWithFormat:@"https://apiurl.com/v1/classes/%@.json", classID];
NSURL *classUrl = [NSURL URLWithString:classUrlString];
NSURLRequest *classRequest = [NSURLRequest requestWithURL:classUrl];
NSData *classData = [NSURLConnection sendSynchronousRequest:classRequest returningResponse:nil error:nil];
NSDictionary *classDictionary = [NSJSONSerialization JSONObjectWithData:classData
options:NSJSONReadingMutableContainers
error:nil];
//NSLog(@"classes dictionary: %@", classDictionary);
if (classes == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showNetworkError];
});
return;
} else {
if (classDictionary == nil) {
NSLog(@"classes dictionary: %@", classDictionary);
} else {
//probably should check here if array is empty before accessing first object
//we shove the data from a classDictionary into class
//class[@"teacher_full_name"] = classDictionary[@"teacher"][0];
if (classDictionary[@"teacher_full_name"] == nil) {
class[@"teacher_full_name"] = @"Not Provided";
} else {
class[@"teacher_full_name"] = classDictionary[@"teacher_full_name"];
}
if (classDictionary[@"course"] == nil) {
class[@"course"] = @"Not Provided";
} else {
class[@"course"] = classDictionary[@"course"];
}
if (classDictionary[@"class_pk"] == nil) {
class[@"class_pk"] = @"Not Provided";
} else {
class[@"class_pk"] = classDictionary[@"class_pk"];
}
if (classDictionary[@"teachers"][0][@"person_fk"] == nil) {
class[@"person_fk"] = @"Not Provided";
} else {
class[@"person_fk"] = classDictionary[@"teachers"][0][@"person_fk"];
}
ScheduleResult *searchResult = [[ScheduleResult alloc] init];
searchResult.name = class[@"course"];
searchResult.teacher = class[@"teacher_full_name"];
searchResult.class_fk = class[@"class_pk"];
[searchResults addObject:searchResult];
NSLog(@"searchresult: %@", class[@"person_fk"]);
//NSLog(@"course: %@ and teacher: %@", class[@"course"], class[@"teacher_full_name"] );
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(@"dictionary: %@", classes);
[self.tableView reloadData];
[SVProgressHUD dismiss];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});