如題。
使用AFN請求數據時,頁面不顯示數據,debug的時候發現dataArray數組沒有值,但是調用testJson這個方法的時候,的確是請求到有數據的,並且已經將數據解析並添加到dataArray這個數組中的啊,不知道為什麼到viewDidLoad這個方法中數組dataArray就是為空。不知道是我寫的代碼的加載順序有問題還是項目的哪個地方需要配置下?求高手幫忙看下。。。
#import "ViewController.h"
#import "AFNetworking.h"
#import "Model.h"
#import "Cell.h"
@interface ViewController ()
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,copy) NSString *url;
@end
@implementation ViewController
-(void)testJSON{
//請求數據的地址
NSString *path = self.url;
//創建管理類
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//設置數據二進制,數據格式默認json
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//利用方法請求數據
[manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSArray *movieArray = dic[@"subjects"];
for (NSDictionary *dict in movieArray)
{
Model *model = [[Model alloc] init];
model.movieName = dict[@"title"];
model.movieYear = dict[@"year"];
model.movieImage = dict[@"images"][@"large"];
[self.dataArray addObject:model];
}
//刷新表
[_tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
#pragma mark ---------數據源方法-------
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Model *model = self.dataArray[indexPath.row];
static NSString *ID = @"ID";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.name.text = model.movieName;
cell.year.text = model.movieYear;
return cell;
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
[self.tableView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellReuseIdentifier:@"Cell"];
static NSString *ID = @"ID";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
你這兩個地方的cellReuseIdentifier都不一樣,cellForRowAtIndexPath連cell都取不到。