#define MAC @"http://127.0.0.1/1506C.json"
NSURL *url=[NSURL URLWithString:MAC];
NSURLSession *see=[NSURLSession sharedSession];
NSURLSessionDataTask *tas=[see dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
[tab reloadData];
});
}];
[tas resume];
tab =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
tab.delegate=self;
tab.dataSource=self;
[self.view addSubview:tab];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dic.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
NSString *key=[dic.allKeys objectAtIndex:section];
return [[dic objectForKey:key]count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
NSString *key=[dic.allKeys objectAtIndex:indexPath.section];
cell.textLabel.text=[[[dic objectForKey:key]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text=[[[dic objectForKey:key]objectAtIndex:indexPath.row]objectForKey:@"like"];
return cell;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [dic.allKeys objectAtIndex:section];
}