model .h
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperationManager.h" @interface testModel : NSObject
@property (nonatomic, strong) NSString *code; @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *status; @property (nonatomic, assign) int time;
@property (nonatomic, strong) NSString *date; - (NSMutableArray *)getData:(UITableView *)tableView;
@end
model.m
1 #import "testModel.h" @implementation testModel - (NSMutableArray *)getData:(UITableView *)tableView{
NSMutableArray *data_array = [[NSMutableArray alloc] init];
7 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"www.hao123.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id reponseObject) {
9 //使用打印出的字符串在网上json解析出数据结构,定义模型属性和获取方法,data数据结构外层为数组,使用数组接收
NSLog(@"%@",[operation responseString]);
NSArray *array = reponseObject[@"data"]; for (NSDictionary *dict in array) {
testModel *test = [[testModel alloc] init];
[test setValuesForKeysWithDictionary:dict];
[data_array addObject:test];
}
[tableView reloadData]; // 数据的接收完成是在页面显示完成之后 --> 一定要使用reloadData刷新控件的数据,
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
NSLog(@"%@", error);
}];
return data_array;
} -(NSString *)description {
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 ];
}

ViewController.m
 #import "testModel.h"
@interface ViewController () <UIScrollViewDelegate>
{
NSMutableArray *data_array;
}
@property (nonatomic ,strong) UITableView *tabV;
@end @implementation ViewController -(void)setTabV:(UITableView *)tabV {
if (_tabV == nil) {
testModel *model = [[testModel alloc] init];
_tabV = tabV;
data_array = [model getData:_tabV];
}
} - (void)viewDidLoad {
[super viewDidLoad];
} #pragma mark------UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
[self setTabV:tableView];
return ; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellStr = @"cellStr";
SCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
if (!cell)
{
cell = [[NSBundle mainBundle]loadNibNamed:@"SCTableViewCell" owner:nil options:nil][];
NSLog(@"******%@******", data_array);
}
return cell;
} -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return tableView.height */;
}

遇到问题一:

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

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

遇到问题二:

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

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

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

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

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

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

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

在模型中获取网络数据,刷新tableView的更多相关文章

  1. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

  2. Http方式获取网络数据

    通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...

  3. [置顶] 获取网络数据中的数组显示成ListView的简单流程

    首先说一下  这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...

  4. Swift实战-豆瓣电台(三)获取网络数据

    观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...

  5. android—获取网络数据

    取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子.   大家都知道,网络通信,发送请求有两种 ...

  6. Linux 中的网络数据包捕获

    Linux 中的网络数据包捕获 Ashish Chaurasia, 工程师 简介: 本教程介绍了捕获和操纵数据包的不同机制.安全应用程序,如 VPN.防火墙和嗅探器,以及网络应用程序,如路由程序,都依 ...

  7. 使用NSURLSession获取网络数据和下载文件

    使用NSURLSession获取网络数据 使用NSURLSession下载文件

  8. Swift - 异步获取网络数据封装类

    使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据.下面通过对数据获取类进行封装,演示如何进行数据请求与接收. 1,HttpContr ...

  9. 通过DialogFragment从DatePicker或TimePicker中获取日期数据

    通过DialogFragment从DatePicker或TimePicker中获取日期数据 一个activity类,里面存有date和time的变量,想通过dialogfragment的方式获取用户输 ...

随机推荐

  1. Spring Batch系列总括(转载)

    最近一个项目在使用SpringBatch框架做一个电子商务平台的批处理.网上资料很有限,尤其是中文资料更是少之又少,官网上的文档也只是讲一些入门的基础知识,大部分高级特性都是一笔带过,讲解的很不彻底, ...

  2. eclipse 中使用等宽字体 inconsolata

    一直以来,就感觉使用 eclipse 时的那几种字体很难看,而且非等宽,空格宽度很小,排版很乱. 搜索并试用了一下,发现了字体inconsolata. 这是一个很适合编程的字体,效果如下: 非常漂亮. ...

  3. Trailing return types

    Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...

  4. 使用VisualStudio进行单元测试之三

    私有方法需不需要测试,本文不做讨论.假设您也认为有时候,私有方法也需要进行测试,那就一起来看看如何进行私有方法的测试. 准备测试代码 测试用的代码还是前面测试时使用过的代码,不同之处就是在类中增加了一 ...

  5. DD-WRT相关资源

    版本网站下载:Other Downloads,进入betas->2014 FTP下载:如ftp://ftp.dd-wrt.com/betas/2014/06-23-2014-r24461/ Re ...

  6. Spoj 7001 Visible Lattice Points 莫比乌斯,分块

    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193   Visible Lattice Points Time L ...

  7. kickStart脚本

    kickstart是什么        许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart,系统管理员可以 ...

  8. there be 句型

    there be 意思:表示存在或者发生. 英文释义:used to show that sth exists or happens 如果现在进行时,单数时用is,复数时用are. 现在我问你一个问题 ...

  9. spark 启动时候报 Unable to load native-hadoop library for your platform 警告

    hadoop2.6.4 jdk1.8 spark2.0.1 方案1: 在spark的conf目录下,修改spark-env.sh文件加入LD_LIBRARY_PATH环境变量,值为hadoop的nat ...

  10. C#类型 分类: C# 2015-03-09 08:44 202人阅读 评论(0) 收藏

    C# 类型 引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又 ...