ios yymodel 解析 post请求

导入第三方 yymodel、afn 记得开网

model.h


NS_ASSUME_NONNULL_BEGIN


@interface SmallModel : NSObject
//数据接口
@property(nonatomic , strong)NSString *detail;
//@property(nonatomic , strong)NSString *author_name;
//@property(nonatomic , strong)NSString *url;

@end

@interface ResultModel : NSObject
//第二层接口
@property(nonatomic , strong)NSString *total;
@property(nonatomic , strong)NSString *size;
@property(nonatomic , strong)NSArray *list;

@end

@interface ZhouKaoModel : NSObject
//第一层接口
@property(nonatomic , strong)NSString *reason;
@property(nonatomic , strong)ResultModel *result;
@end```

model.m
```@implementation SmallModel

//+ (NSDictionary *)modelCustomPropertyMapper {
//
//    return@{@"cataID" :@"id"};
//
//}


@end

@implementation ResultModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"list":[SmallModel class]};
}

@end
@implementation ZhouKaoModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"result":[ResultModel class]};
}
@end```

view.h
```#import "ZhouKaoModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface ZhouKaoTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *title;
-(void)loadDataFromModel:(SmallModel *)model;

@end```

view.m
```-(void)loadDataFromModel:(SmallModel *)model{
    if (model) {
        //[self.ImgV sd_setImageWithURL:[NSURL URLWithString:model.clientcover1]];
        self.title.text = model.detail;
        //self.imgSumLabel.text = [NSString stringWithFormat:@"%@图",model.imgsum];
        //self.replynumLabel.text = [NSString stringWithFormat:@"%@评论",model.replynum];
    }
}```

controller.h
```#import "ViewController.h"
#import "ZhouKaoModel.h"
#import "AFNetworking.h"
#import "ZhouKaoTableViewCell.h"

@interface ViewController ()
{
    //UITableView *table;
    //NSMutableArray *array;
    ZhouKaoModel *model;
}
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic , strong)UITableView *table;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"地图" style:UIBarButtonItemStyleDone target:self action:@selector(map)];

    
    _table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    
    //XIB形式创建的TableViewCell
    [self.table registerNib:[UINib nibWithNibName:@"ZhouKaoTableViewCell" bundle:nil] forCellReuseIdentifier:@"title"];
    
    [self.view addSubview:_table];
    self.array = [NSMutableArray array];

    
    
    [self jiexi];

    
    
}



-(void)jiexi{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //NSDictionary *dict = @{@"key":@"6d96ee265cc090b418ee51257ff9c48f",@"lat":@"40.041478",@"lon":@"116.300267"};
    // POST请求
    [manager POST:@"http://v.juhe.cn/wzpoints/query?key=6d96ee265cc090b418ee51257ff9c48f&lat=40.041478&lon=116.300267" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
        self->model = [ZhouKaoModel yy_modelWithJSON:responseObject];
       // [self.array addObject:self->model.result.list];
       // self.array = self->model.result.list;
        NSLog(@"网络请求成功");
        //NSLog(@"%@",[self->model.result.list[0]detail]);
        //刷新表格
        [self.table reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //点语法
    return model.result.list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
//    }

 ZhouKaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"title" forIndexPath:indexPath];

    SmallModel *mod = [model.result.list objectAtIndex:indexPath.row];
    [cell loadDataFromModel:mod];

    //cell.textLabel.text = mod.detail;
    //cell.detailTextLabel.text = mod.author_name;
    return cell;
}

@end

你可能感兴趣的:(ios yymodel 解析 post请求)