在模型中获取网络数据,刷新tableView

 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 }

遇到问题一:

数据的接收在页面限制之后,在外部打印数据为空

解决:刷新控件数据,在控件代理方法中可获取数据

遇到问题二:

getdata在viewcontroller中,不利于的控制器和数据的分离

解决:将控件当作参数传入getDatazhong

      遇到问题:将调用getData放在ViewDidLoad中,防止重复调用,但是进入getData时,参数为空,reloadData之后,不再进入ViewDidLoad方法,而是直接进入tableView的代理方法

      解决:在代理方法中调用getData

               遇到问题:重复调用getData,重复刷新tabelView --> 死循环

               解决:使用懒加载,全局tableView,在全局变量为空时赋值并调用getData

将关于数据的代码放入模型中,微调整,运行成功!

原文地址:https://www.cnblogs.com/LiuLady12138/p/4723520.html