代碼如下,如何在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"
}
]
}
[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展示出來了