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. java.sql.SQLException: Can not issue executeUpdate() for SELECTs

    未处理的多个select语句 解决方法就是:查看有没有用了同一个连接来处理多个SQL语句!

  2. Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题

    在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统.应用程序之间.应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是 ...

  3. Linux 关机命令 重启命令

    Linux centos重启命令: 1.reboot2.shutdown -r now 立刻重启(root用户使用)3.shutdown -r 10 过10分钟自动重启(root用户使用)4.shut ...

  4. Spark如何解决常见的Top N问题

    需求   假设我们有一张各个产品线URL的访问记录表,该表仅仅有两个字段:product.url,我们需要统计各个产品线下访问次数前10的URL是哪些?   解决方案   (1)模拟访问记录数据   ...

  5. HDU-4974 A simple water problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 话说是签到题,我也不懂什么是签到题. A simple water problem Time Limit: ...

  6. Ural 1519. Formula 1 优美的插头DP

    今天早上学了插头DP的思想和最基础的应用,中午就开始敲了,岐哥说第一次写不要看别人代码,利用自己的理解一点点得写出来,这样才锻炼代码能力!于是下午慢慢地构思轮廓,一点点地敲出主体代码,其实是很磨蹭的, ...

  7. Java中的成员初始化顺序和内存分配过程

    Java中的成员初始化顺序和内存分配过程 原帖是这样描述的: http://java.dzone.com/articles/java-object-initialization?utm_source= ...

  8. 帮助招聘程序员的自动考试网站:Codility

    Automated tests of programming skills. Assessment of software developers. Recruitment software. Codi ...

  9. cocos2d与cocos2d-X中的draw和update

    像其它的游戏引擎一样,我们有两个不同的方法来完成draw和update. 1: Draw:每一个CCNode都有一个draw方法,每一帧都会调用.我们只在这个方法里做描绘的事情. 2: Update: ...

  10. Python3.x爬虫教程:爬网页、爬图片、自己主动登录

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 摘要:本文将使用Python3.4爬网页.爬图片.自己主动登录.并对HTTP协议做了一个简单 ...