2013-02-14 160 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]; 
} 
相關問題