美文网首页
1.5 AFNetworking解析 案列展示

1.5 AFNetworking解析 案列展示

作者: 草根小强 | 来源:发表于2019-04-18 12:03 被阅读0次

    AFNetworking解析 案列展示

    #import "ViewController.h"
    #import "AFNetworking.h"
    #import "LQTableViewCell.h"
    #import "Model.h"
    #define BASE_URL @"http://iappfree.candou.com:8080/free/applications/limited"
    #define qfCell @"qfCell"
    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
    // 界面显示
    @property (nonatomic, strong) UITableView *tableView;
    
    // 数据源数组
    @property (nonatomic, strong) NSMutableArray *dataSource;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        // 实例化tableView
        [self dataSource];
        [self customTableView];
    }
    
    
    - (void)GET{
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        //无参get的请求
        [manager GET:BASE_URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的回调
            NSLog(@"%@",responseObject);
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //请求错误的回调 (失败的回调)
            
            NSLog(@"%@",error);
        }];
        
        
    }
    /** 实例化tableView */
    - (void)customTableView {
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        [self.view addSubview:self.tableView];
        
       
        // 自定义cell
        [self.tableView registerClass:[LQTableViewCell class] forCellReuseIdentifier:qfCell];
    }
    
    
    #pragma mark ------ 协议方法 ---------
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        return self.dataSource.count;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        LQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:qfCell];
        
        // 每行要显示的数据
        Model *model = self.dataSource[indexPath.row];
        
    #warning 这是关键的一步
        // 把要显示的内容, 给视图做显示
        cell.model = model;
        
        
        return cell;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        LQTableViewCell *lqCell = (LQTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        
        return lqCell.cellHeight;
    }
    
    
    #pragma mark -------- 懒加载 ---------
    - (NSMutableArray *)dataSource {
        if (_dataSource == nil) {
            
            _dataSource = [[NSMutableArray alloc] init];
            
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
            //无参get的请求
            [manager GET:BASE_URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                // 请求成功的回调
                NSLog(@"%@",responseObject);
                NSArray *allData = [responseObject objectForKey:@"applications"];
                
                for (NSDictionary *dict in allData) {
                    // 转成模型, 存到数据源数组中
                    Model *model = [Model modelWithDict:dict];
                    [_dataSource addObject:model];
                }
                NSLog(@"%@",_dataSource);
                [self.tableView reloadData];
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                //请求错误的回调 (失败的回调)
                
                NSLog(@"%@",error);
            }];
            
            
        }
        
        return _dataSource;
    }
    @end
    

    创建模型

    
    #import <Foundation/Foundation.h>
    
    @interface Model : NSObject
    
    /** 名字 */
    @property (nonatomic, copy) NSString *name;
    
    /** 简介 */
    @property (nonatomic, copy) NSString *descriptionOne;
    
    /**  */
    @property (nonatomic, copy) NSString *categoryName;
    
    + (Model *)modelWithDict:(NSDictionary *)dict;
    @end
    
    
    #import "Model.h"
    
    @implementation Model
    
    + (Model *)modelWithDict:(NSDictionary *)dict{
        Model *model = [[Model alloc] init];
        
    //    [model setValuesForKeysWithDictionary:dict];
        model.name = dict[@"name"];
        model.descriptionOne = dict[@"description"];
        model.categoryName = dict[@"categoryName"];
        return model;
        
    }
    @end
    
    #import <UIKit/UIKit.h>
    
    @class Model;
    @interface LQTableViewCell : UITableViewCell
    @property (nonatomic,strong) Model * model;
    @property (nonatomic,assign)CGFloat cellHeight;
    @end
    
    

    自定义cell

    #import "LQTableViewCell.h"
    #import "UIView+frame.h"
    #import "Constant.h"
    #import "Model.h"
    
    @interface LQTableViewCell()
    
    @property (nonatomic,strong)UILabel *labelName;
    
    @property (nonatomic,strong)UILabel *labelDescriptionOne;
    @property (nonatomic,strong)UIButton * button;
    @property (nonatomic,assign)BOOL isHot;
    @end
    
    
    @implementation LQTableViewCell
    
    - (void)setModel:(Model *)model{
        
        self.labelName.text = model.name;
        self.labelDescriptionOne.text = model.descriptionOne;
    //    self.button
        CGFloat height = [model.descriptionOne boundingRectWithSize:CGSizeMake(self.labelDescriptionOne.width, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.labelDescriptionOne.font} context:nil].size.height;
        self.labelDescriptionOne.height = height;
        self.button.y = self.labelDescriptionOne.maxY + 10;
        self.cellHeight = self.button.maxY + 10;
        _model = model;
        
        
    }
    - (UILabel *)labelName{
        
        if (_labelName == nil){
            
            _labelName = [[UILabel alloc] init];
            
            _labelName.x  = 10;
            _labelName.y = 10;
            
            _labelName.width = [[UIScreen mainScreen] bounds].size.width - 10;
            _labelName.height = 20;
            [self.contentView addSubview:_labelName];
        }
        NSLog(@"打印了名字");
        return _labelName;
        
        
    }
    
    - (UILabel *) labelDescriptionOne{
        
        if (_labelDescriptionOne == nil) {
            
    //        _labelDescriptionOne = [[UILabel alloc] init];
    //        
    //        _labelDescriptionOne.x = 10;
    //        _labelDescriptionOne.y = self.labelName.maxY + 10;
    //        _labelDescriptionOne.width = [[UIScreen mainScreen] bounds].size.width - 20;
    //        _labelDescriptionOne.height = 0;
            _labelDescriptionOne = [[UILabel alloc]init];
              _labelDescriptionOne.frame = CGRectMake(self.labelName.frame.origin.x, self.labelName.maxY+10, SCREENWIDTH - 20, 100);
              _labelDescriptionOne.font = [UIFont systemFontOfSize:13];
            
              _labelDescriptionOne.textColor = UIColorRGBA(255, 138, 156, 1.0);
              _labelDescriptionOne.numberOfLines = 0;
            [self.contentView addSubview:_labelDescriptionOne];
            
            
        }
        NSLog(@"打印了内容");
        return _labelDescriptionOne;
        
    }
    
    - (UIButton *)button{
        if (_button == nil) {
    //        _button = [[UIButton alloc] init];
    //        
    //        _button.x = 10;
    //        _button.width = [[UIScreen mainScreen] bounds].size.width -20;
    //        _button.height = 30;
            _button = [[UIButton alloc]initWithFrame:CGRectMake(self.labelName.frame.origin.x, self.labelDescriptionOne.maxY+10, SCREENWIDTH - 20, 30)];
            
            [_button setTitle:@"赞" forState:UIControlStateNormal];
            
            [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
            
            _button.backgroundColor = [UIColor redColor];
            
            [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_button];
            NSLog(@"打印了button");
        }
        
        return _button;
    }
    //
    - (void)buttonAction:(UIButton *)button{
        
        NSString *str = [NSString stringWithFormat:@"您已成功为%@点赞",_labelName.text];
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate: self  cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alertView show];
        
        
        
    }
    @end
    
    
    AFNetworking解析 案列展示.png

    相关文章

      网友评论

          本文标题:1.5 AFNetworking解析 案列展示

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