// 全局变量
NSDictionary *_dic;
// 创建表格
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
// 数据请求
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];
// 设置表格
-(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];
}