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条)

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'