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. GPL,LGPL和BSD等协议注意事项

    使用开源软件必须注意GPL,LGPL和BSD等协议 简而言之,GPL协议就是一个开放源代码协议,软件的初始开发者使用了GPL协议并公开软件的源程序后,后续使用该软件源程序开发软件者亦应当根据GPL协议 ...

  2. vector,list,deque容器的迭代器简单介绍

    我们知道标准库中的容器有vector,list和deque.另外还有slist,只不过它不是标准容器.而谈到容器,我们不得不知道进行容器一切操作的利器---迭代器.而在了解迭代器之前,我们得先知道每个 ...

  3. python换行写入文件

    今天用python做写入文件时,碰到,写入的东西不能换行,打开写入的文件都是一行.后来发现需要在写入的字符后面加上+'\n'. 另外python需要追加写入文件的时候,是用这个方法f = open(' ...

  4. Linux驱动的两种加载方式过程分析

    一.概念简述 在Linux下可以通过两种方式加载驱动程序:静态加载和动态加载. 静态加载就是把驱动程序直接编译进内核,系统启动后可以直接调用.静态加载的缺点是调试起来比较麻烦,每次修改一个地方都要重新 ...

  5. (转载)shell中用date命令获取昨天、明天或者多天前的日期

    (转载)http://blog.sina.com.cn/s/blog_3e4774e30100p0yv.html 使用date命令获取日期很方便,最近需要获取当前日期的下一天日期在linux应该如何获 ...

  6. Delphi 编写的Web Service

      一编写服务程序 第一步:File----->New----->Other------>WebServices----->Soap Server Application选择I ...

  7. CentOS下安装gcc和gdb

    我的操作系统是CentOS6.4,安装源里自带了gcc4.4.0和gdb7.0,版本略老遂删除之重新安装. gcc 1.下载源码包,解压 //下载 wget http: //ftp.gnu.org/g ...

  8. iframe 中嵌套刷新

    if(top.frames.length>0){top.location.href = window.location.href;}

  9. 415. Add Strings

    没什么限定的话,先翻转,在一位一位加,记得进位就行了.. public class Solution { public String addStrings(String num1, String nu ...

  10. UVa11526 H(n)

    http://blog.csdn.net/synapse7/article/details/12873437 #include<cstdio> #include<cstring> ...