开发者社区> 问答> 正文

请问IOS中怎么将开源库AFNetworking的GET函数中获取的数据,在函数体外利用呢?

代码如下,如何在GET函数外获取到在其内解析到的数据,从而加以利用呢?

 //
//  ViewController.m
//  1
//
//  Created by   on 16-2-2.
//  Copyright (c) 2016年  . All rights reserved.
//

#import "ViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "XiaHuaModel.h"
#import "XiaoHuaCell.h"


@interface ViewController ()
{
   __block NSMutableArray *arrData;
    UITableView *xiaoHuaView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *url = @"http://localhost/json/2.json";

    xiaoHuaView = [[UITableView alloc]initWithFrame:self.view.bounds];
    xiaoHuaView.delegate = self;
    xiaoHuaView.dataSource = self;
    [self.view addSubview:xiaoHuaView];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSError *err= nil;
        //XiaHuaModel用于解析JSON数据为对象,它继承自第三方开源库JSONModel
        XiaHuaModel *xiaHua =  [[XiaHuaModel alloc]initWithDictionary:(NSDictionary *)responseObject error:&err];
        NSArray *detailArray = xiaHua.detail;
        arrData = [NSMutableArray arrayWithArray:detailArray];
        // detailModel *detail = [detailArray objectAtIndex:indexPath.row];
        // NSDictionary *detailDictionary = [detail toDictionary];
        NSLog(@"AFNetworking GET 函数内部数据为: %d",arrData.count);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error...%@",error);
    }];

     NSLog(@"AFNetworking GET 函数外部数据为: %d",arrData.count);

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection函数中的数据为 : %d",arrData.count);
        //此处期望获得值为2,但是却得到0
    return arrData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"XiaoHuaCell";
    XiaoHuaCell *cell = (XiaoHuaCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[XiaoHuaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    return cell;

}

@end

**执行的结果为(执行的顺序与返回值不知是如何得到的,请指教)
2016-02-06 05:35:36.347 1[1945:60b] AFNetworking GET 函数外部数据为: 0
2016-02-06 05:35:36.365 1[1945:60b] numberOfRowsInSection函数中的数据为 : 0
2016-02-06 05:35:36.481 1[1945:60b] AFNetworking GET 函数内部数据为: 4
**
该段代码中 http://localhost/json/2.json 中的数据如下:
  {
  "status": "000000",
  "desc": null,
  "detail": [
    {
      "id": 40052,
      "xhid": 40052,
      "author": "不要抓我",
      "content": "有多少人,当年在学校为了夜里出去上网锯窗户过?",
      "picUrl": "",
      "status": "1"
    },
    {
      "id": 40051,
      "xhid": 40051,
      "author": "咸鱼翻跟头",
      "content": "一个人在外面打工,手伤了缝了5针也不敢跟家人说,怕她们担心,一个人硬撑着,可心里怎么那么难过啊!",
      "picUrl": "http://img.appd.lengxiaohua.cn/2016/01/31/bd82974300ced_o.jpg",
      "status": "1"
    }
  ]
}

展开
收起
爵霸 2016-03-06 13:29:09 2286 0
1 条回答
写回答
取消 提交回答
  •  [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
            NSError *err= nil;
            //XiaHuaModel用于解析JSON数据为对象,它继承自第三方开源库JSONModel
            XiaHuaModel *xiaHua =  [[XiaHuaModel alloc]initWithDictionary:(NSDictionary *)responseObject error:&err];
            NSArray *detailArray = xiaHua.detail;
            arrData = [NSMutableArray arrayWithArray:detailArray];
            // detailModel *detail = [detailArray objectAtIndex:indexPath.row];
            // NSDictionary *detailDictionary = [detail toDictionary];
            NSLog(@"AFNetworking GET 函数内部数据为: %d",arrData.count);
                    //注意这一句
                    [xiaoHuaView reloaddata];
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error...%@",error);
        }];
     NSLog(@"AFNetworking GET 函数外部数据为: %d",arrData.count);

    其实也就是在你Get到数据之后,调用一下tableview的刷新,tableview就会重新加载数据,那么你新的数据就能被tableview展示出来了

    2019-07-17 18:54:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载