shoutuan 2016-02-05 13:49 采纳率: 100%
浏览 1595
已采纳

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"
    }
  ]
}
  • 写回答

4条回答

  • 林深 2016-02-05 16:34
    关注
         [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展示出来了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站