我使用 AFNetworking 用 youtube 视频填充 UITableView。YouTube API 只允许每个请求最多 50 个结果。所以我必须使用多个 URL 来获得超过 50 个结果。
我创建了一个方法,它将在给定的 URL 上执行 AFNetworkings AFJSONRequestOperation。
我认为 UITable 是在我收到 JSON 数据之前创建的。在我创建方法之前,一切都运行良好。
这是我第一次创建一个方法。在过去的几天里,我一直在尝试在 UITable 上加载 50 多个 youtube 视频。请查看我的代码。
这是我的代码,你也可以从这里下载整个项目
QQViewController.m
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//[super viewDidLoad];
NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50";
// I am not sure how i am supposed to populate the uitableview with the second link :(
NSString *urlAsString2 = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50&start-index=51";
self.myurl = [NSURL URLWithString:urlAsString];
[self getJSONfromURL:self.myurl];
[self.tableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.videoMetaData = [self.myJSON valueForKeyPath:@"items.video"];
NSLog(@" QQVC video Meta Data %@", self.videoMetaData);
self.allThumbnails = [self.myJSON valueForKeyPath:@"data.items.video.thumbnail"];
// The table need to be reloaded or else we will get an empty table.
[self.tableView reloadData]; // Must Reload
// NSLog(@" video Meta Data %@", self.videoMetaData);
}
// 这里是方法
-(void)getJSONfromURL:(NSURL *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// setup AFNetworking stuff
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// call delegate or processing method on success
// [self.myJSON = (NSArray *)JSON];
self.myJSON = [JSON valueForKey:@"data"];
[self.tableView reloadData];
//NSLog(@" in get JSon method %@", self.myJSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}