网络接口请求


#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;


}

你可能感兴趣的:(网络接口请求)