I using AFNetworking to populate a UITableView with youtube videos. YouTube API only allows maximum 50 result for each request. So I have to use multiple URLs to get more than 50 results.
I created a Method which will do AFNetworkings AFJSONRequestOperation on the given URLs.
I think the UITable is getting created before i receive the JSON data. Everything was working perfectly before i created the Method.
This the first time i have created a Method. I have been trying to load more than 50 youtube videos on a UITable for the last few days.Please have a look at my code.
Here is my code, you can also download the entire project from
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);
}
// Here is the Method
-(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];
}
Move your reload tableview call to here.