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];
}
thanx很多!有用!! – user2960280
歡迎您接受答案:) –