// 全局變量
NSDictionary *_dic;
// 創(chuàng)建表格
?self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
// 數(shù)據(jù)請(qǐng)求
? ? NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:(JSON_URL)] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// jsonkit解析
? ? ? ? _dic= [data objectFromJSONData];
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [self.tableViewreloadData];
? ? ? ? });
? ? }];
? ? [taskresume];
// 設(shè)置表格
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
? ? return_dic.count;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
? ? return [[_dic objectForKey:[_dic.allKeys objectAtIndex:section]] count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
? ? if(!cell) {
? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
? ? }
? ? NSArray*arr = [_dicobjectForKey:[_dic.allKeysobjectAtIndex:indexPath.section]];
? ? cell.textLabel.text= [arr[indexPath.row]objectForKey:@"name"];
? ? cell.detailTextLabel.text= [arr[indexPath.row]objectForKey:@"like"];
? ? return cell;
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
? ? return [_dic allKeys][section];
}