AFNetworking 解析json 数据

创建 Model 类

定义属性


-(void)loadData{


    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];

    manger.responseSerializer = [AFHTTPResponseSerializer serializer];

    [mangerGET:@"http://127.0.0.1/Book.json"parameters:nilprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"成功");


    NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:1error:nil];

        for(NSString* keyindic) {

            [self.titleArrayaddObject:key];

            NSMutableArray *arr = [[NSMutableArray alloc]init];

            for(NSDictionary*dictindic[key]) {


                BookModel*model = [[BookModelalloc]init];

                [modelsetValuesForKeysWithDictionary:dict];

                [arraddObject:model];

            }

            [self.arrayaddObject:arr];

        }


        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tbVreloadData];

        });


    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        NSLog(@"失败");

    }];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return self.titleArray.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return[self.array[section]count];

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }



    cell.textLabel.text= [self.array[indexPath.section][indexPath.row]title];

    cell.detailTextLabel.text=[NSStringstringWithFormat:@"作者:%@ 价格%@",[self.array[indexPath.section][indexPath.row]author],[self.array[indexPath.section][indexPath.row]price]];


    returncell;

}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

    returnself.titleArray[section];

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,384评论 3 38
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 11,008评论 3 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,817评论 1 32
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,632评论 9 5
  • 怎么个过瘾呢?就像喝酒不上头,抽烟不伤肺,打飞机不肾虚。
    蓝星英语阅读 118评论 0 0

友情链接更多精彩内容