ios 分区解析

继承Object     .h 写文本属性 名


#import "ViewController.h"

#import "AFNetworking"


#import "BookModel.h"

@interface ViewController ()

@property(nonatomic,strong) UITableView * tableView;

@property(nonatomic,strong) NSMutableArray * array;

@property(nonatomic,strong) NSMutableArray * titleArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    self.tableView.delegate=self;

    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];


    self.array = [NSMutableArray new];

    self.titleArray = [NSMutableArray new];

    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [managerGET:@"http://127.0.0.1/Book%E7%9A%84%E5%89%AF%E6%9C%AC.json"parameters:nilprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"ok");


        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil];


        for (NSString  * keyindic) {


            [self.titleArrayaddObject:key];

            NSMutableArray * arr = [NSMutableArray new];

            for (NSDictionary* dic2indic[key]) {


                BookModel* model = [BookModelnew];

                [modelsetValuesForKeysWithDictionary:dic2];

                [arraddObject:model];

            }

            [self.arrayaddObject:arr];

        }


        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableViewreloadData];

        });

    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"no");

    }];


}

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

    return[self.array[section]count];

}

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

    return self.titleArray.count;

}

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

    returnself.titleArray[section];

}

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

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

    if (cell==nil) {

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

    }

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

    cell.detailTextLabel.text=[self.array[indexPath.section][indexPath.row]author];

    returncell;

}

@end

你可能感兴趣的:(ios 分区解析)