2013-02-06 50 views
2

我正在使用AFNetworking解析UITableView上的youtube JSON數據。空的UITableView同時解析JSON與AFNetworking

由於某種原因,我得到一個空的UITable。我不知道我做錯了什麼。

請看看我的代碼:

這裏是Google Drive

#import "KKViewController.h" 

#import "AFNetworking.h" 

#import "AFJSONRequestOperation.h" 

@interface KKViewController() 

@end 

@implementation KKViewController 



- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50"; 

    NSURL *url = [NSURL URLWithString:urlAsString]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 


    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 



     self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"]; 

     // NSLog(@" video Meta Data %@", self.videoMetaData); 





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

    [operation start]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 



    return [self.videoMetaData count]; 

} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 
    } 


    NSDictionary *titles = [self.videoMetaData objectAtIndex:indexPath.row]; 

    NSLog(@"%@",titles); 

    cell.textLabel.text = [titles objectForKey:@"title"]; 

    return cell; 
} 

回答

3

您是否嘗試過重新加載數據加載後表格視圖中的項目?

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSLog(@"JSON = %@", JSON); 
    self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"]; 
    NSLog(@" video Meta Data %@", self.videoMetaData); 
    [self.tableView reloadData]; // Now I need to reload the table view data. 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
}]; 
+0

大聲笑非常感謝。我花了最後幾個小時試圖找出問題:S – user1951876

2

你需要[self.tableview reloadData];這將解決問題..