美文网首页
获取接口数据

获取接口数据

作者: 朕i | 来源:发表于2018-09-10 08:18 被阅读0次

    Model.h

    #import

    @interface Model : NSObject

    @property (nonatomic,strong)NSString *author;

    @property (nonatomic,strong)NSString *create_time;

    @property (nonatomic,strong)NSString *img;

    @property (nonatomic,strong)NSString *title;

    -(NSArray*)getModelArr:(NSArray*)jsonData;

    @end

    Model.m

    #import "Model.h"

    @implementation Model

    -(NSArray*)getModelArr:(NSArray*)jsonData{

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

        //遍历数组存model

        for(NSDictionary*dicinjsonData)

        {

            Model*model = [[Modelalloc]init];

    //        model.author = [dic valueForKey:@"author"];

            //给属性赋值

            [modelsetValuesForKeysWithDictionary:dic];

            //把model存入数组

        }

        return[modelArrcopy];

    }

    - (void)setValue:(id)value forUndefinedKey:(NSString*)key

    {

    }

    @end

    viewController.m

    #import "ViewController.h"

    #import "AFNetworking.h"

    #import "UIImageView+WebCache.h"

    #import "Model.h"

    #import "MyTableViewCell.h"

    @interface ViewController ()

    {

        UITableView *tabV;

    }

    @property (nonatomic,strong)NSArray *dataArr;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

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

        tabV.delegate=self;

        tabV.dataSource = self;

        [self.view addSubview:tabV];

        NSArray*arr =@[@"1",@"2"];

        NSMutableArray*mArr = [arrmutableCopy];

        //网址字符串

        NSString *urlStr = @"http://buluokes.huimor.com/api";

        //参数字典

        NSDictionary *params = @{@"method":@"app.user.homepage"};

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

        [managerPOST:urlStrparameters:paramsprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

            NSLog(@"%@",responseObject);

            Model*model = [[Modelalloc]init];

            NSDictionary*dataDic = [responseObjectvalueForKey:@"data"];

            NSArray*listArr = [dataDicvalueForKey:@"list"];

            self->_dataArr=  [modelgetModelArr:listArr];

            NSLog(@"%@",self->_dataArr);

            dispatch_async(dispatch_get_main_queue(), ^{

                [self->tabVreloadData];

            });

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

        }];

    }

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

        return self.dataArr.count;

    }

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

        staticNSString*str =@"cell";

        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

        if(!cell){

            cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];

        }

        //取model

        Model*model =_dataArr[indexPath.row];

        NSLog(@"%@",model.title);

        //把图片网址封装为URL

        NSURL*imgUlr = [NSURLURLWithString:model.img];

        [cell.imgsd_setImageWithURL:imgUlr];

        cell.title.text= model.title;

        cell.author.text= model.author;

        cell.creat_Time.text = model.create_time;

        returncell;

    }

    相关文章

      网友评论

          本文标题:获取接口数据

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