我仍然不习惯使用NSURL来获取数据,并且在尝试使用它时似乎有问题。在这种情况下,我使用debug来检查ViewDidload中的所有日期,并且所有正确的数据都会进入并分成我希望用来构建表视图控制器的数组。但是当我们在section方法中达到NumberOfRows时,所有数组似乎都被重置为nil。 我已经尝试过使用NSURL解决方案的各种组合,但似乎没有比我现在使用的更多(至少显示一些数据令人难以置信)。任何人都可以告诉我,如果我犯了一个明显的错误,或者如果没有给我一段可靠的代码,我应该用它来执行这样的简单GET。 非常感谢你。
以下是我的代码:
@implementation MyLessonsTableViewController
NSArray *pastarr = nil;
NSArray *todoarr = nil;
NSArray *comingarr = nil;
NSArray *jsonless = nil;
- (void)viewDidLoad {
[super viewDidLoad];
// GET MY LESSONS FROM DATABASE
jsonless = [[NSArray alloc] init];
pastarr = [[NSArray alloc] init];
todoarr = [[NSArray alloc] init];
comingarr = [[NSArray alloc] init];
NSString *token = @"5cfd28bed3f5f5bd63143c81a50d434a";
NSString *urlString = [NSString stringWithFormat:@"http://soon.nextdoorteacher.com/apps/api/nextdoorteacher/student-lessons?t=%@", token];
NSURL *urlcc = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:urlcc];
NSError *error;
NSMutableDictionary *jsonLess = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
NSLog(@"My Lessons Json == %@", jsonLess);
// SPLIT ARRAY
NSArray *pastarr = [jsonLess valueForKeyPath:@"past"];
NSArray *todoarr = [jsonLess valueForKeyPath:@"todo"];
NSArray *comingarr = [jsonLess valueForKeyPath:@"upcoming"];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSUInteger lessonRowCount = 0;
switch (section) {
case 0:
lessonRowCount = todoarr.count;
break;
case 1:
lessonRowCount = comingarr.count;
break;
case 2:
lessonRowCount = pastarr.count;
break;
default:
break;
}
return lessonRowCount;
}
答案 0 :(得分:1)
有几个问题。
reloadData
。dispatch_async
reloadData
之前致电jsonLess
。此处发布的代码全部修好:
@implementation MyLessonsTableViewController {
NSArray *pastarr = nil;
NSArray *todoarr = nil;
NSArray *comingarr = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
// GET MY LESSONS FROM DATABASE
NSString *token = @"5cfd28bed3f5f5bd63143c81a50d434a";
NSString *urlString = [NSString stringWithFormat:@"http://soon.nextdoorteacher.com/apps/api/nextdoorteacher/student-lessons?t=%@", token];
NSURL *urlcc = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:urlcc];
NSError *error;
NSMutableDictionary *jsonLess = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
error:&error];
NSLog(@"My Lessons Json == %@", jsonLess);
// SPLIT ARRAY
pastarr = [jsonLess valueForKeyPath:@"past"];
todoarr = [jsonLess valueForKeyPath:@"todo"];
comingarr = [jsonLess valueForKeyPath:@"upcoming"];
[self.tableView reloadData];
}
现在这仍然存在一个大问题。您正在主线程上进行Internet访问。那很糟糕。你真的应该这样做:
- (void)viewDidLoad {
[super viewDidLoad];
// GET MY LESSONS FROM DATABASE
NSString *token = @"5cfd28bed3f5f5bd63143c81a50d434a";
NSString *urlString = [NSString stringWithFormat:@"http://soon.nextdoorteacher.com/apps/api/nextdoorteacher/student-lessons?t=%@", token];
NSURL *urlcc = [NSURL URLWithString:urlString];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:urlcc];
NSError *error;
NSMutableDictionary *jsonLess = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions
error:&error];
NSLog(@"My Lessons Json == %@", jsonLess);
// SPLIT ARRAY
pastarr = [jsonLess valueForKeyPath:@"past"];
todoarr = [jsonLess valueForKeyPath:@"todo"];
comingarr = [jsonLess valueForKeyPath:@"upcoming"];
// Now this must be done on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}};
}