在模型中获取网络数据,刷新tableView
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的更多相关文章
- Android中获取网络数据时的分页加载
//此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载, 蓝色部分是睡眠时间,自我感觉不用写 ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...
- Http方式获取网络数据
通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...
- [置顶] 获取网络数据中的数组显示成ListView的简单流程
首先说一下 这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...
- Swift实战-豆瓣电台(三)获取网络数据
观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...
- android—获取网络数据
取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子. 大家都知道,网络通信,发送请求有两种 ...
- Linux 中的网络数据包捕获
Linux 中的网络数据包捕获 Ashish Chaurasia, 工程师 简介: 本教程介绍了捕获和操纵数据包的不同机制.安全应用程序,如 VPN.防火墙和嗅探器,以及网络应用程序,如路由程序,都依 ...
- 使用NSURLSession获取网络数据和下载文件
使用NSURLSession获取网络数据 使用NSURLSession下载文件
- Swift - 异步获取网络数据封装类
使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据.下面通过对数据获取类进行封装,演示如何进行数据请求与接收. 1,HttpContr ...
- 通过DialogFragment从DatePicker或TimePicker中获取日期数据
通过DialogFragment从DatePicker或TimePicker中获取日期数据 一个activity类,里面存有date和time的变量,想通过dialogfragment的方式获取用户输 ...
随机推荐
- 凯撒密码 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/凯撒密码 题解:刚开始想map, ...
- CentOS下安装gcc和gdb
我的操作系统是CentOS6.4,安装源里自带了gcc4.4.0和gdb7.0,版本略老遂删除之重新安装. gcc 1.下载源码包,解压 //下载 wget http: //ftp.gnu.org/g ...
- SRM 388(1-250pt)
题意:定义一个数为k-smooth,如果它最大的质因子不超过k.给定n和k,求不超过n的,k-smooth的数有多少个.(k <= 100, n <= 10^5) 解法:对于一个数t,判断 ...
- SRM 601(1-250pt,500pt)
DIV1 250pt 题意:有很多袋子,里面装有苹果和橘子(也可能没有),给出每个袋子里有多少个苹果,多少个橘子.如果每个袋子里含有水果的总数都不小于x个,则可以从每个袋子里都拿出x个水果(拿出苹果和 ...
- socket programming Max size of tcp/ip socket Buffer?
TCP data is buffered at both sender and receiver. The size of the receiver's socket receive buffer d ...
- 开源存储之ceph
小记,曾经的很多单骑,赵子龙,杨再兴,..............为大将者所应用的胆识和气度,值得敬仰! 名师出高徒啊, 周侗北宋末年之武术大师,相传为三国姜维的传人(真实性ruiy哥就不考察了哈), ...
- ASP.NET与SOAP协议使用记录
近期初次接手一个公司的管理系统开发任务,因为公司需要有Android,IOS客户端,又要求有PC端的网页客户端....对服务请求的要求自然也就落在了统一接口访问上了.... 使用ASP.NET的WEB ...
- nginx浏览pdf
location ~/M00{ # root /fdfs/storage/data; # if ($req ...
- HTTP 返回时间 概念 TTFB..
课外学习部分: 什么是TTFB呢? 1.TTFB (Time To First Byte),是最初的网络请求被发起到从服务器接收到第一个字节这段时间,它包含了 TCP连接时间,发送HTTP请求时间和获 ...
- 【设计模式 - 9】之装饰者模式(Decorator)
1 模式简介 装饰者模式允许向一个现有的对象添加新的功能,同时又不改变其结构. 装饰者模式的思路是用"调料"对象将原始对象进行层层包裹,同时其属性.动作层层传递,达到最终 ...