我使用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];
}
获得下载后,您需要再次在表视图上调用reloadData。 – rdelmar 2013-02-14 02:38:19
我没有重新加载viewDidAppear – user1951876 2013-02-14 02:40:37
但这可能为时尚早。每当您的下载方法返回时,您都需要再次执行此操作。 – rdelmar 2013-02-14 03:06:47