AFNetworking下载数据Demo

原创
2015/08/26 11:05
阅读数 68

#import "ConnectionController.h"

#define kContentUrl @"http://223.6.252.214/weibofun/weibo_list.php?apiver=10500&category=weibo_pics&page=1&page_size=30&max_timestamp=-1"


@interface ConnectionController ()


@end


@implementation ConnectionController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //self.view.backgroundColor=[UIColor redColor];

    [self createtableView];

    [self createRequest];

    [self loadDataWithPage];

    

}


//创建tableView

-(void)createtableView

{

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

    self.tableView.delegate=self;

    self.tableView.dataSource=self;

    [self.tableView registerNib:[UINib nibWithNibName:@"ConnectionCell" bundle:nil] forCellReuseIdentifier:@"ConnectionCell"];

    [self.view addSubview:self.tableView];

}

//创建数据源

-(void)createRequest{

    _manager=[AFHTTPRequestOperationManager manager];

    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];

    self.dataArr=[[NSMutableArray alloc]init];

}


//下载解析数据

-(void)loadDataWithPage{

    

    __weak typeof (self)weakSelf=self;

#if 1

    [_manager GET:kContentUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

#else

    //注册登录的post请求 aa表示账号所对应的nametextfield.text  bb表示账号所对应的posswordtextfield.text

    NSString *str=[NSString stringWithFormat:@"name=%@&password=%@",aa,bb];

    [_manager POST:kContentUrl parameters:str success:^(AFHTTPRequestOperation *operation, id responseObject) {

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        

    }];

#endif


        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

        NSArray *items=dict[@"items"];

        for (NSDictionary *dictInfo in items) {

            ConnectionModel *model=[[ConnectionModel alloc]init];

#if 1

            [model setValuesForKeysWithDictionary:dictInfo];

#else

            model.wbody=dictInfo[@"wbody"];

            model.wpic_middle=dictInfo[@"wpic_middle"];

            model.update_time=dictInfo[@"update_time"];

#endif

            [weakSelf.dataArr addObject:model];

        }

        [weakSelf.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"下载失败");

    }];

}


//实现tebelView协议中的方法

#pragma mark -tebelView协议中的方法

//设置有多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

//设置每组中cell的个数

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

{

    return self.dataArr.count;

}

//填充cell

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

{

    ConnectionCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ConnectionCell" forIndexPath:indexPath];

    ConnectionModel *model=self.dataArr[indexPath.row];

    [cell showDataWithModel:model];

    return cell;

}

//设置cell的高度

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

{

    return 300;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


展开阅读全文
打赏
0
0 收藏
分享

作者的其它热门文章

加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部