2015-08-20 23 views
1

通過NSURLSession讀取數據我嘗試使用NSURLSession讀取數據並返回上實現代碼如下,但即使我的代碼在viewArray增加了數據的上的tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [viewArray count]; 
} 

返回0!這裏是我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Initialize table data 
    viewArray = [NSMutableArray arrayWithObjects: nil ]; 

    NSURLSession *session = [NSURLSession sharedSession]; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"http://pizzy.sadrcom.com/foodCategories/pizzas/margarita"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     NSLog(@"%@", json); 
     for (NSDictionary* groups in json) { 
      GroupData *gInfo = [[GroupData alloc] init]; 
      // Title 
      gInfo.title = [groups objectForKey:@"Title"]; 
      // The Id 
      gInfo.theId = [groups objectForKey:@"Id"]; 

      [viewArray addObject:gInfo]; 
     } 

    }]; 
    [dataTask resume]; 
} 

回答

2

在將對象添加到數組後,您不會調用[tableView reloadData];,並且當您獲取數據異步時,您的numberOfRowsInSection將在數據收到之前首先調用。 做這樣的事情

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Initialize table data 
    viewArray = [NSMutableArray arrayWithObjects: nil ]; 

    NSURLSession *session = [NSURLSession sharedSession]; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"http://pizzy.sadrcom.com/foodCategories/pizzas/margarita"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
    NSLog(@"%@", json); 
    for (NSDictionary* groups in json) { 
     GroupData *gInfo = [[GroupData alloc] init]; 
     // Title 
     gInfo.title = [groups objectForKey:@"Title"]; 
     // The Id 
     gInfo.theId = [groups objectForKey:@"Id"]; 
     [viewArray addObject:gInfo]; 

    } 
     dispatch_async(dispatch_get_main_queue(), ^{ 
       [tableView reloadData]; 
      }); 
    }]; 
    [dataTask resume]; 

}

+1

thanx很多!有用!! – user2960280

+0

歡迎您接受答案:) –

1

這是因爲比創建界面的響應是後話。試試這個

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Initialize table data 
    viewArray = [NSMutableArray alloc]init];//replace your initialisation with this. Yours is uglier 

    NSURLSession *session = [NSURLSession sharedSession]; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"http://pizzy.sadrcom.com/foodCategories/pizzas/margarita"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     NSLog(@"%@", json); 
     for (NSDictionary* groups in json) { 
      GroupData *gInfo = [[GroupData alloc] init]; 
      // Title 
      gInfo.title = [groups objectForKey:@"Title"]; 
      // The Id 
      gInfo.theId = [groups objectForKey:@"Id"]; 

      [viewArray addObject:gInfo]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 

       [yourTable reloadData];//replace yourTable with your table variable. 
      }); 

     } 

    }]; 
    [dataTask resume]; 
}