1 model .h 2 #import <Foundation/Foundation.h> 3 #import "AFHTTPRequestOperationManager.h" 4 5 @interface testModel : NSObject 6 @property (nonatomic, strong) NSString *code;11 @property (nonatomic, strong) NSString *name;13 @property (nonatomic, strong) NSString *status;15 @property (nonatomic, assign) int time; 16 @property (nonatomic, strong) NSString *date; 17 18 - (NSMutableArray *)getData:(UITableView *)tableView; 19 @end
model.m
1 #import "testModel.h" 2 3 @implementation testModel 4 5 - (NSMutableArray *)getData:(UITableView *)tableView{
6 NSMutableArray *data_array = [[NSMutableArray alloc] init];
7 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
8 [manager GET:@"www.hao123.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id reponseObject) {
9 //使用打印出的字符串在網上json解析出數據結構,定義模型屬性和獲取方法,data數據結構外層爲數組,使用數組接收
10 NSLog(@"%@",[operation responseString]);
11 NSArray *array = reponseObject[@"data"];
12
13 for (NSDictionary *dict in array) {
14 testModel *test = [[testModel alloc] init];
15 [test setValuesForKeysWithDictionary:dict];
16 [data_array addObject:test];
17 }
18 [tableView reloadData]; // 數據的接收完成是在頁面顯示完成以後 --> 必定要使用reloadData刷新控件的數據,
19 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
20 NSLog(@"%@", error);
21 }];
22 return data_array;
23 }
24
25 -(NSString *)description {
26 return [NSString stringWithFormat:@"<%@, %p>self.code = %@, self.name = %@, self.status = %@, self.time = %d, self.date = %@",self.class, self, self.code, self.name, self.status, self.time, self.date ];
27 }
ViewController.m
1 #import "testModel.h" 2 @interface ViewController () <UIScrollViewDelegate> 3 { 4 NSMutableArray *data_array; 5 } 6 @property (nonatomic ,strong) UITableView *tabV; 7 @end 8 9 @implementation ViewController 10 11 -(void)setTabV:(UITableView *)tabV { 12 if (_tabV == nil) { 13 testModel *model = [[testModel alloc] init]; 14 _tabV = tabV; 15 data_array = [model getData:_tabV]; 16 } 17 } 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 } 22 23 #pragma mark------UITableViewDataSource,UITableViewDelegate 24 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 25 { 26 [self setTabV:tableView]; 27 return 1; 28 29 } 30 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 31 { 32 static NSString *cellStr = @"cellStr"; 33 SCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr]; 34 if (!cell) 35 { 36 cell = [[NSBundle mainBundle]loadNibNamed:@"SCTableViewCell" owner:nil options:nil][0]; 37 NSLog(@"******%@******", data_array); 38 } 39 return cell; 40 } 41 42 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 43 { 44 return tableView.height *18/45; 45 }
遇到問題一:json
數據的接收在頁面限制以後,在外部打印數據爲空數組
解決:刷新控件數據,在控件代理方法中可獲取數據數據結構
遇到問題二:atom
getdata在viewcontroller中,不利於的控制器和數據的分離spa
解決:將控件看成參數傳入getDatazhong代理
遇到問題:將調用getData放在ViewDidLoad中,防止重複調用,可是進入getData時,參數爲空,reloadData以後,再也不進入ViewDidLoad方法,而是直接進入tableView的代理方法code
解決:在代理方法中調用getDataorm
遇到問題:重複調用getData,重複刷新tabelView --> 死循環blog
解決:使用懶加載,全局tableView,在全局變量爲空時賦值並調用getDataip
將關於數據的代碼放入模型中,微調整,運行成功!