美文网首页
NSURLSession

NSURLSession

作者: 法库德 | 来源:发表于2017-08-30 10:00 被阅读0次

    #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];

    }

    相关文章

      网友评论

          本文标题: NSURLSession

          本文链接:https://www.haomeiwen.com/subject/lbuzdxtx.html