2013-02-14 132 views
0

我使用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]; 
} 
+1

获得下载后,您需要再次在表视图上调用reloadData。 – rdelmar 2013-02-14 02:38:19

+0

我没有重新加载viewDidAppear – user1951876 2013-02-14 02:40:37

+1

但这可能为时尚早。每当您的下载方法返回时,您都需要再次执行此操作。 – rdelmar 2013-02-14 03:06:47

回答

0

移动你重装tableview中调用到这里。

-(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]; 



} 
+0

nope。仍然是空桌子。 – user1951876 2013-02-14 03:14:59

+0

然后你可能没有正确解析JSON。 – endy 2013-02-14 03:16:20

+0

可以在控制台上看到JSON。没有什么不妥。 – user1951876 2013-02-14 03:19:58

1

getJSONfromURL:方法有几个问题。主要问题在于你将self.myJSON定义为[JSON valueForKey:@“data”],但随后将self.allThumbnails定义为[self.myJSON valueForKeyPath:@“data.items.video.thumbnail”] - 您使用过“数据”再次,所以self.thumbnails是空的。这似乎工作正常:

-(void)getJSONfromURL:(NSURL *)url { 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    // setup AFNetworking stuff 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     self.myJSON = [JSON valueForKey:@"data"]; 
     self.allThumbnails = [self.myJSON valueForKeyPath:@"items.video.thumbnail"]; 
     self.videoMetaData = [self.myJSON valueForKeyPath:@"items.video"]; 
     [self.tableView reloadData]; 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
    }]; 

    [operation start]; 
} 

如果你想从多个URL加载日期,你可以这样做。我添加了一个计数器来跟踪通过URL字符串数组的进度。该表在每次下载后都会重新加载,因此在显示一些数据之前不必等待整个过程完成。

@interface QWViewController() 
@property (strong,nonatomic) NSArray *urlStrings; 
@end 

@implementation QWViewController { 
    int counter; 
} 


-(void)viewDidLoad { 
    [super viewDidLoad]; 
    counter = 0; 
    NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50"; 
    NSString *urlAsString2 = @"http://gdata.youtube.com/feeds/api/playlists/PL7CF5B0AC3B1EB1D5?v=2&alt=jsonc&max-results=50&start-index=51"; 
    self.urlStrings = @[urlAsString,urlAsString2]; 
    self.allThumbnails = [NSMutableArray array]; 
    self.videoMetaData = [NSMutableArray array]; 
    [self getJSONFromURL:self.urlStrings[0]]; 
} 


-(void)getJSONFromURL:(NSString *) urlString { 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     self.myJSON = [JSON valueForKey:@"data"]; 
     [self.allThumbnails addObjectsFromArray:[self.myJSON valueForKeyPath:@"items.video.thumbnail"]]; 
     [self.videoMetaData addObjectsFromArray:[self.myJSON valueForKeyPath:@"items.video"]]; 
     [self.tableView reloadData]; 
     counter += 1; 
     if (counter < self.urlStrings.count) [self getJSONFromURL:self.urlStrings[counter]]; 
    } 

    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
    }]; 
    [operation start]; 
} 
相关问题