练习

//

//  Model.h

//  模拟

//

//  Created by 与你共度 on 2018/12/29.

//  Copyright © 2018 与你共度. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject

@property(nonatomic,strong)NSString *photo;

@property(nonatomic,strong)NSString *title;

@property(nonatomic,strong)NSString *suitIntro;

@property(nonatomic,strong)NSString *readNum;

@end

NS_ASSUME_NONNULL_END






//

//  MyTableViewCell.h

//  模拟

//

//  Created by 与你共度 on 2018/12/29.

//  Copyright © 2018 与你共度. All rights reserved.

//

#import

@class Model;

@interfaceMyTableViewCell :UITableViewCell

@property(nonatomic ,strong)Model *m;

@end






//

//  MyTableViewCell.m

//  模拟

//

//  Created by 与你共度 on 2018/12/29.

//  Copyright © 2018 与你共度. All rights reserved.

//

#import "MyTableViewCell.h"

#import "Model.h"

#import "UIImageView+WebCache.h"

@interface MyTableViewCell ()

@property(nonatomic,strong)UIImageView *imgV;

@property(nonatomic,strong)UILabel *zhuTitle;

@property(nonatomic,strong)UILabel *fuTitle;

@property(nonatomic,strong)UILabel *readTitle;

@end

@implementationMyTableViewCell

//添加子控件

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{

    if(self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {

        [selfaddSubview:self.imgV];

        [selfaddSubview:self.zhuTitle];

        [selfaddSubview:self.fuTitle];

        [selfaddSubview:self.readTitle];

    }


    return self;

}

//懒加载

-(UIImageView *)imgV{

    if(!_imgV) {

        _imgV= [[UIImageViewalloc]init];


    }

    return _imgV;

}

-(UILabel*)zhuTitle{

    if(!_zhuTitle) {

        _zhuTitle= [[UILabelalloc]init];



    }

    return _zhuTitle;

}

-(UILabel*)fuTitle{

    if(!_fuTitle) {

        _fuTitle= [[UILabelalloc]init];

    }


    return _fuTitle;

}

-(UILabel*)readTitle{

    if (!_readTitle) {


        _readTitle= [[UILabelalloc]init];

    }


    return _readTitle;

}

//布局控件

-(void)layoutSubviews{


    [super layoutSubviews];


    self.imgV.frame=CGRectMake(5,5,100,100);

    self.zhuTitle.frame=CGRectMake(110,20,40,30);

    self.fuTitle.frame=CGRectMake(110,70,60,30);

    self.readTitle.frame = CGRectMake(300,100,20,20);

}

- (void)awakeFromNib {

    [superawakeFromNib];



    // Initialization code

}

-(void)setM:(Model *)m{

    _m = m;

    [self.imgV sd_setImageWithURL:[NSURL URLWithString:m.photo]];

    self.zhuTitle.text = m.title;

    self.fuTitle.text = m.suitIntro;

    self.readTitle.text = m.readNum;



}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [supersetSelected:selected animated:animated];

    // Configure the view for the selected state

}

@end



//

//  AppDelegate.h

//  模拟

//

//  Created by 与你共度 on 2018/12/29.

//  Copyright © 2018 与你共度. All rights reserved.

//

#import

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end






//

//  ViewController.m

//  模拟

//

//  Created by 与你共度 on 2018/12/29.

//  Copyright © 2018 与你共度. All rights reserved.

//

#import "ViewController.h"

#import "AFNetworking/AFNetworking.h"

#import "MJExtension/MJExtension.h"

#import "Model/Model.h"

#import "View/MyTableViewCell.h"

#define quanjuPath @"http://124.65.238.30:3300/bingyun/api/"

#define path @"strategy/page"

@interface ViewController ()

@property(nonatomic,strong)UITableView *tab;

@property(nonatomic,strong)NSMutableArray *dataSouace;

@end

@implementation ViewController

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

    return self.dataSouace.count;

}

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

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

    if(!cell) {

        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }

    //创建模型

    Model*m =self.dataSouace[indexPath.row];

    cell.m= m;


    returncell;


}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 150;

}

- (void)viewDidLoad {

    [super viewDidLoad];


    self.dataSouace = [NSMutableArray array];


    self.tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tab.delegate=self;

    self.tab.dataSource = self;

    [self.viewaddSubview:self.tab];


    //创建网络请求管理者

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //拼接url

    NSString*str = [NSStringstringWithFormat:@"%@%@",quanjuPath,path];


    //将参数包装成字典

    NSDictionary *dic = @{

                          @"pageSize":@"10",

                          @"strategyType":@"POSTURE",

                          @"pageNo":@"1"

                          };



    [managerGET:strparameters:dicsuccess:^(NSURLSessionDataTask*task,idresponseObject) {


        //处理服务器数据

        NSArray*arr = responseObject[@"data"];

        for(NSDictionary*dicinarr) {


            //字典转模型

            Model*m = [Modelmj_objectWithKeyValues:dic];


            [self.dataSouaceaddObject:m];

        }


        //刷新表格

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tabreloadData];

        });


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


    }];


    // Do any additional setup after loading the view, typically from a nib.

}

@end

你可能感兴趣的:(练习)