自己对网络请求进行封装,block作参数

//
//  RootViewController.m
//  MVC
//
//  Created by  on 15/8/5.
//  Copyright (c) 2015   Congwang. All rights reserved.
//

#import  "RootViewController.h"
#import
  "HomePageTableView.h"
#import
  "NetWorkingEngine.h"
#import
  "ContentModel.h"
@interface  RootViewController  ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)HomePageTableView *homePageViewTV;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation RootViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
 //初始化数组
   
 self.dataSource = [NSMutableArray array];
   
 self.homePageViewTV = [[HomePageTableView alloc]initWithFrame:[UIScreen mainScreen].boundsstyle:UITableViewStylePlain];
   
 self.homePageViewTV.delegate = self;
   
 self.homePageViewTV.dataSource = self;
   
 //注册cell
    [
self.homePageViewTV registerClass:[UITableViewCell class]forCellReuseIdentifier:@"cell"];
   
 //添加视图
    [
self.view addSubview:self.homePageViewTV];
    [[
NetWorkingEngine shardNetWorkingEngine]getInfoFromServerWithUrlStr:@"http://m2.qiushibaike.com/article/list/text?count=30&page=" withSuccess:^(NSData *response){
       
 NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
       
 NSArray *itemsArray = dic[@"items"];
       
 NSLog(@"%@",itemsArray);
       
 for (NSDictionary *userDic in itemsArray) {
           
 ContentModel *model = [[ContentModel alloc]initWithDic:userDic];
            [
self.dataSource addObject:model];
        }
//tableView刷新
        [self.homePageViewTV  reloadData];
    }  withFailure :^( NSData  *error) {
  
             NSLog ( @" 哈哈 , 网速不给力 " );
      

            }];
       
  // Do any additional setup after loading the view.
}
#pragma mark -- UITableViewDataSource 的方法
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{
   
  return  1 ;
}
- (
NSInteger )tableView:( UITableView  *)tableView numberOfRowsInSection:( NSInteger )section{
   
  return  self . dataSource . count ;
}

- (
UITableViewCell  *)tableView:( UITableView  *)tableView cellForRowAtIndexPath:( NSIndexPath  *)indexPath{
   
  UITableViewCell  *cell = [tableView  dequeueReusableCellWithIdentifier : @"cell"  forIndexPath :indexPath];
   
   
  ContentModel  *model =  self . dataSource [indexPath. row ];
    cell.
textLabel . text  = model. contentStr ;
    cell.
textLabel . numberOfLines  =  0 ;
   
  return  cell;
   
}



- (
void )didReceiveMemoryWarning {
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}


@end

NetWorkingEngine.h

//
//  NetWorkingEngine.h
//  MVC
//
//  Created by lanouhn on 15/8/5.
//  Copyright (c) 2015 Congwang. All rights reserved.
//

#import 

@interface NetWorkingEngine : NSObject
+ (
NetWorkingEngine *)shardNetWorkingEngine;

//请求数据的方法

//block 做参数 :(void (^)(NSData *response))block 的类型
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr withSuccess:(void (^)(NSData *response))success withFailure:(void (^)(NSError *error))failure;
@end


NetWorkingEngine.m

//
//  NetWorkingEngine.m
//  MVC
//
//  Created by  on 15/8/5.
//  Copyright (c) 2015 Congwang. All rights reserved.
//

#import "NetWorkingEngine.h"

@implementation NetWorkingEngine
//单例方法的实现
+ (
NetWorkingEngine *)shardNetWorkingEngine{
   
 static NetWorkingEngine *netWorkEngine = nil;
   
 //GCD - 单例写法
   
 //在每个线程下都执行一次
   
 static dispatch_once_t onceToken;
   
 dispatch_once(&onceToken, ^{
        netWorkEngine = [[
NetWorkingEngine alloc] init];
    });
   
 return netWorkEngine;
   
}
// 请求数据的方法  -- 封装系统的请求数据的方法
- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr withSuccess:(void (^)(NSData *))success withFailure:(void (^)(NSError *))failure{
    // 1.创建url对象
   
 NSURL *url = [NSURL URLWithString:urlStr];
   
 //2.创建请求对象
   
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
   
 //3.通过connection发起请求
    [
NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse*response, NSData *data, NSError *connectionError) {
       
 //执行到此, 说明请求结束
       
 if (data) {
           
 //调用block
            success(data);
           
       
        }
        failure(error);
       
    }];
   
   
}
@end


ContentModel.h


//
//  ContentModel.h
//  MVC
//
//  Created by  on 15/8/5.
//  Copyright (c) 2015 Congwang. All rights reserved.
//

#import 

@interface ContentModel : NSObject
// 内容属性
@property (nonatomic, copy)NSString *contentStr;
- (
id)initWithDic:(NSDictionary *)dic;



@end

ContentModel.m

//
//  ContentModel.m
//  MVC
//
//  Created by  on 15/8/5.
//  Copyright (c) 2015 Congwang. All rights reserved.
//

#import "ContentModel.h"

@implementation ContentModel
-(
id)initWithDic:(NSDictionary *)dic{
   
 if (self = [super init]) {
       
 self.contentStr = dic[@"content"];
    }
return self;
 }
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值