美文网首页
网络接口请求

网络接口请求

作者: uuuuaa37 | 来源:发表于2018-08-25 00:13 被阅读0次


#import "wodeViewController.h"

#import "wodeTableViewCell.h"

#import "LinkageView.h"

#import "define的副本.h"

#import "defind的副本.h"

#import "UIScrollView+MJRefreshEX.h"

@interface wodeViewController ()

{

    UIScrollView* scrollV;

    NSDictionary*dic,*resultdic;

    NSMutableArray* dataArr;

    UIPageControl* page,*page2;

    UITableView *tbv;

    NSArray*arr;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    arr = @[@"1",@"1",@"1"];

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

    tbv.delegate=self;

    tbv.dataSource = self;

    [self.view addSubview:tbv];

    [tbv addHeaderWithHeaderClass:nil beginRefresh:YES delegate:self animation:YES];

}

- (void)onRefreshing:(id)control {

    [self requestNetWorkingWithPageNum:1 isHeader:YES];

}

- (void)onLoadingMoreData:(id)control pageNum:(NSNumber*)pageNum {

    [self requestNetWorkingWithPageNum:pageNum.integerValue isHeader:NO];

}

- (void)requestNetWorkingWithPageNum:(NSInteger)pageNum isHeader:(BOOL)isHeader

{

    //解析数据

    NSString * str = [TEST_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURLSession * session = [NSURLSession sharedSession];

    //定义一个字典接收数据

    dic= [[NSDictionaryalloc]init];

    resultdic= [[NSDictionaryalloc]init];

    dataArr = [[NSMutableArray alloc]init];

    NSURLSessionTask* task = [sessiondataTaskWithURL:[NSURLURLWithString:str]completionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {

        //开始解析

        self->dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

//        NSLog(@"122%@",self->dic);

        self->resultdic=self->dic[@"result"];

//        NSLog(@"asd%@",resultdic);

        self->dataArr = resultdic[@"data"];

//        NSLog(@"%@",dataArr);

        //存入数组并同步

//        [[NSUserDefaults standardUserDefaults] setObject:resultdic[@"data"] forKey:@"mutableArr"];

//

//        [[NSUserDefaults standardUserDefaults] synchronize];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self->tbvreloadData];

            [self->tbv endHeaderRefreshWithChangePageIndex:YES];

        });

    }];

    //发送请求

    [taskresume];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return 2;

}

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

{

    if(section ==0){

        return1;

    }else{

        returndataArr.count;

    }

}

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

{

    NSString* str = [NSStringstringWithFormat:@"cell%ld",(long)indexPath.row];

    wodeTableViewCell* cell  = [tableViewdequeueReusableCellWithIdentifier:str];

    if(!cell) {

        cell = [[wodeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];

    }

    if(indexPath.section==0)

    {

        //初始化滚动视图

        scrollV= [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,120)];

        //设置滚动范围

        scrollV.contentSize = CGSizeMake(self.view.frame.size.width * 3 , 120);

        //设置代理

        scrollV.delegate=self;

        //设置是否分页

        scrollV.pagingEnabled=YES;

        //取消弹簧效果

        scrollV.bounces=NO;

        //取消滑动

        scrollV.showsHorizontalScrollIndicator=NO;

        //添加到视图

        [tbvaddSubview:scrollV];

        for(inti =0; i<3; i++) {

            UIImageView* imgV = [[UIImageViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width* i,0,self.view.frame.size.width,160)];

            imgV.image= [UIImageimageNamed:arr[i]];

            [scrollVaddSubview:imgV];

        }

        page= [[UIPageControlalloc]initWithFrame:CGRectMake((self.view.frame.size.width-150)/2,130,150,30)];

        //设置点数

        page.numberOfPages = 3;

        //设置颜色

        page.pageIndicatorTintColor = [UIColor blackColor];

        //设置滑过颜色

        page.currentPageIndicatorTintColor = [UIColor whiteColor];

        //添加到视图

        [tbvaddSubview:page];

        returncell;

    }else

    {

        tbv.rowHeight = SCREEN_RATE_640_WIDTH(600);

        NSDictionary* onedic =dataArr[indexPath.row];

        cell.label.text= onedic[@"title"];

        cell.date.text= onedic[@"date"];

        cell.name.text= onedic[@"author_name"];

        NSData* imgDate = [NSDatadataWithContentsOfURL:[NSURLURLWithString:onedic[@"thumbnail_pic_s"]]];

        cell.imgV.image= [UIImageimageWithData:imgDate];

        returncell;

    }

}

//行高

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

{

    if(indexPath.section==0)

    {

        return120;

    }

    elseif(indexPath.section==1)

    {

        return150;

    }

    return 0;

}

//滚动视图偏移量

-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{

    page.currentPage = scrollV.contentOffset.x/self.view.frame.size.width;

}

相关文章

  • Retrofit 基础篇

    一、Retrofit 网络请求接口的注解类型:网络请求方法、标记类、网络请求参数 (1)网络请求方法 (2)标记类...

  • 网络请求(接口)

    项目里网络请求是必不可少的,框架使用的是AFNetworking三方库。在其基础上做了一层封装,你使用到的是JMR...

  • 小程序请求API接口,网络请求封装

    概述 前几日关注的用户问我有没有封装好的小程序网络请求接口,那么小程序请求API接口,网络请求封装,我们应该怎么编...

  • 各种网络错误

    1、网络错误[ 0 ] Network Error 异常的网络错误,首先查看后端请求接口,如果请求列表出现了该接口...

  • retrofit 由浅入深(三)源码解析

    源码理解主要从这几个方面 解析网络请求接口的注解配置网络请求参数 动态代理 生成 网络请求对象 网络请求适配器 将...

  • Android16-网络请求的封装

    1. 封装HttpURLConnection的网络请求 首先定义一个接口作为网络请求结果的回调 然后封装网络请求的...

  • Android App异常处理笔记

    请求网络接口时使用RxJava进行链式调用。 一,执行网络请求得到网络Response 如果response的状态...

  • http请求-自定义

    1.网络请求框架 实现 1.线程管理类 单例实现 2.请求的接口 3.请求的返回接口 4.请求的数据返回接口 5....

  • 网络接口请求

    #import "wodeViewController.h" #import "wodeTableViewCell...

  • 网络请求

    ios开发 合理封装请求接口 概述 如今大多的app都会与网络打交道,作为开发者 合理的对网络后台请求接口进行封装...

网友评论

      本文标题:网络接口请求

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