ios中封装网络和tableview的综合运用
1:封装网络请求 类
- #import <Foundation/Foundation.h>
- #import "ASIFormDataRequest.h"
- #import "Reachability.h"
- @protocol NetWorkDelegate;
- @interface JSNetWord : NSObject<ASIHTTPRequestDelegate>
- +(id)ShareNetwork;
- -(void)NetWorkWithConnctId:(int)connectid body:(NSString *)body PostBody:(NSString *)PostBody aDelegate:(id<NetWorkDelegate>)delegate;
- @end
- @protocol NetWorkDelegate <NSObject>
- -(void)NetWorkWithConnectId:(int)connectid aBackString:(NSString *)backString withState:(int) state;
- @end
- #import "JSNetWord.h"
- @implementation JSNetWord
- +(id)ShareNetwork{
- static dispatch_once_t onceToken;
- static JSNetWord *netWork=nil;
- dispatch_once(&onceToken, ^{
- netWork=[[JSNetWord alloc] init];
- });
- return netWork;
- }
- //网络请求
- -(void)NetWorkWithConnctId:(int)connectid body:(NSString *)body PostBody:(NSString *)PostBody aDelegate:(id<NetWorkDelegate>)delegate{
- if([self IsConnect]){
- UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:nil message:@"请联网" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
- [alertview show];
- [alertview release];
- if(delegate && [delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:withState:)]){
- [delegate NetWorkWithConnectId:connectid aBackString:nil withState:];
- }
- return;
- }
- ASIFormDataRequest *Request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://61.177.61.252/szzssw/taxCommon.action?code=1001&pagecode=1&menucode=3300000000"]];
- Request.timeOutSeconds=;
- Request.requestMethod=@"POST";
- Request.delegate=self;
- NSArray *PostBodyArrary=[PostBody componentsSeparatedByString:@"&"];
- for(NSString *temp in PostBodyArrary){
- NSArray *postArray=[temp componentsSeparatedByString:@"="];
- [Request setPostValue:postArray[] forKey:postArray[]];
- }
- //封装参数
- NSString *cid=[NSString stringWithFormat:@"%zi",connectid];
- Request.userInfo=@{@"cid":cid,@"delegate":delegate};
- [Request startAsynchronous];
- }
- #pragma mark -ASIHttpRequest delegate
- -(void)requestStarted:(ASIHTTPRequest *)request{
- //可以加入 toast
- }
- -(void)requestFinished:(ASIHTTPRequest *)request{
- NSDictionary *dic=request.userInfo;
- int connectid= [dic[@"cid"] intValue];
- id<NetWorkDelegate> delegate=dic[@"delegate"];
- if (delegate &&[delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:withState:)]) {
- [delegate NetWorkWithConnectId:connectid aBackString:[ request responseString] withState:];
- }
- }
- -(void)requestFailed:(ASIHTTPRequest *)request{
- NSDictionary *dic=request.userInfo;
- int connectid= [dic[@"cid"] intValue];
- id<NetWorkDelegate> delegate=dic[@"delegate"];
- NSLog(@"错误原因:-->%@",request.error.localizedDescription);
- if(delegate &&[delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:)]){
- [delegate NetWorkWithConnectId:connectid aBackString:[request responseString] withState:];
- }
- UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请求失败" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
- [alertview show];
- [alertview release];
- }
- //判断是否联网
- -(BOOL)IsConnect{
- BOOL IsCon=NO;
- Reachability *r=[Reachability reachabilityWithHostName:@"www.baidu.com"];
- switch ([r currentReachabilityStatus]) {
- case NotReachable:
- NSLog(@"not network");
- IsCon=NO;
- break;
- case ReachableViaWiFi:
- NSLog(@"wifi");
- IsCon=YES;
- break;
- case ReachableViaWWAN:
- NSLog(@"3g");
- IsCon=YES;
- break;
- default:
- break;
- }
- return IsCon;
- }
- @end
封装上下拉tableview .
配置步骤:英文原文和类库下载地址:https://github.com/emreberge/EGOTableViewPullRefresh
- 添加 QuartzCore.framework 到你的工程中。
- 将 EGOTableViewPullRefresh 拖到你的工程目录下。
- 查看 PullTableView.h 文件可用的属性。
- 添加一个PullTableView 到你代码中,实现PullTableViewDelegate委托方法
- #import <UIKit/UIKit.h>
- #import "PullTableView.h"
- @interface JSContentView : UIView<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate>
- -(id)initWithFrame:(CGRect)frame requestSEL:(SEL)aselector aDelegate:(id)delegate;
- @property(nonatomic,assign)int Pagecode;
- @property(nonatomic,readonly)PullTableView *tableview;
- @property(nonatomic,retain)NSMutableArray *mydata;
- @end
- #import "JSContentView.h"
- #import "MyCell.h"
- @interface JSContentView ()
- {
- SEL selector;
- id delegate;
- }
- @end
- @implementation JSContentView
- #pragma mark -初始化
- -(id)initWithFrame:(CGRect)frame requestSEL:(SEL)aselector aDelegate:(id)adelegate{
- self=[super initWithFrame:frame];
- if(self){
- self.mydata=[NSMutableArray array];
- _tableview =[[PullTableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
- _tableview.delegate=self;
- _tableview.dataSource=self;
- _tableview.pullDelegate=self;
- //设置pulltableview 的背景色
- _tableview.pullBackgroundColor=[UIColor clearColor];
- _tableview.pullTextColor=[UIColor blackColor];
- [self addSubview:_tableview];
- selector=aselector;
- delegate=adelegate;
- //第一次加载的时候默认要刷新。
- if(!self.tableview.pullTableIsRefreshing){
- self.tableview.pullTableIsRefreshing=YES;
- if (delegate&&[delegate respondsToSelector:selector]) {
- [delegate performSelector:selector];
- }
- }
- self.tableview.pullTableIsLoadingMore=NO;
- }
- return self;
- }
- - (void)dealloc
- {
- [_mydata release];
- [super dealloc];
- }
- #pragma mark -tableview dataSource
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return ;
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return self.mydata.count;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *cellIdentify=@"myCell";
- MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
- if (cell==nil) {
- cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
- }
- NSDictionary *dic=self.mydata[indexPath.row];
- cell.value=dic;
- return cell;
- }
- #pragma mark -tableview delegate
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return ;
- }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str = [NSStringstringWithFormat:@"%d",indexPath.row];
if(self.delegate && [self.delegate respondsToSelector:@selector(EnterDetailPage:)]){
[self.delegate performSelector:@selector(EnterDetailPage:) withObject:str];
}
}
在viewcontroller里面运用起来。
- #import <UIKit/UIKit.h>
- #import "JSContentView.h"
- #import "JSNetWord.h"
- @interface ViewController : UIViewController<NetWorkDelegate>
- @end
- ===========
- #import "ViewController.h"
- #import "MyCell.h"
- @interface ViewController ()
- {
- JSContentView *newView;
- }
- @property(nonatomic,retain)NSMutableArray *dataArray;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- newView=[[JSContentView alloc] initWithFrame:self.view.bounds requestSEL:@selector(RequestData) aDelegate:self];
- [self.view addSubview:newView];
- [newView release];
- //如果开始启用缓存
- // NSString *cache=[self readApiCacheFileName:@"1.txt"];
- // if (cache.length>0 &&newView.Pagecode<=1) {
- // [self NetWorkWithConnectId:100 aBackString:cache withState:0];
- // }
- }
- -(void)RequestData{
- NSString *url=[NSString stringWithFormat:@"http://61.177.61.252/szzssw/taxCommon.action?code=1001&pagecode=%zi&menucode=3300000000",newView.Pagecode==?:newView.Pagecode];
- //请求数据,重新刷新数据
- [[JSNetWord ShareNetwork] NetWorkWithConnctId: body:url PostBody:nil aDelegate:self];
- //先从缓存中读取
- NSString *cache=[self readApiCacheFileName:@"1.txt"];
- if (cache.length> &&newView.Pagecode==) {
- [self NetWorkWithConnectId: aBackString:cache withState:];
- }
- }
- #pragma mark -network delegate
- -(void)NetWorkWithConnectId:(int)connectid aBackString:(NSString *)backString withState:(int)state{
- if(connectid==){
//1:代表网络 0代表缓存- if (newView.Pagecode<= && state==) {
- newView.tableview.pullTableIsRefreshing=NO;
- }
- if (newView.Pagecode>) {
- newView.tableview.pullTableIsLoadingMore=NO;
- }
- NSDictionary *dic=[backString JSONValue];
- NSString *dealcode=dic[@"dealcode"];
- if (![dealcode isEqualToString:@""]) {
- return;
- }
- if (newView.Pagecode==) {
- [self saveApiCacheFileName:@"1.txt" textContent:backString Append:NO];
- }
- if (newView.Pagecode== &&newView.mydata.count>) {
- [newView.mydata removeAllObjects];
- }
- NSArray *data=dic[@"content"];
- [newView.mydata addObjectsFromArray:data];
- [newView.tableview reloadData];
- }
- }
-(void)EnterDetailPage:(NSString *)row{
int r=[row intValue];
NSDictionary *dic=newView.mydata[r];
NSLog(@"%@",dic[@"title"]);
//可以创建指定controlview。
// ViewController *vc=[[ViewController alloc] init];
// ......
}
- -(void)viewDidUnload{
- [super viewDidUnload];
- self.dataArray=nil;
- }
- - (void)dealloc
- {
- [_dataArray release];
- [super dealloc];
- }
- @end
其中自定义cell
- #import <UIKit/UIKit.h>
- @interface MyCell : UITableViewCell
- @property(nonatomic,retain)NSDictionary *value;
- @end
- #import "MyCell.h"
- #define KTitleFont 15
- #define KFont 12
- #define Kpadding 10
- @interface MyCell (){
- UILabel *titleLb;
- UILabel *Contetlb;
- UILabel *dataLb;
- }
- @end
- @implementation MyCell
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- titleLb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor redColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:KTitleFont]];
- // Contetlb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentRight font:[UIFont systemFontOfSize:KFont]];
- dataLb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:KFont]];
- [self.contentView addSubview:titleLb];
- [self.contentView addSubview:dataLb];
- }
- return self;
- }
- -(void)setValue:(NSDictionary *)value{
- if(_value!=value){
- [_value release];
- _value=[value retain];
- titleLb.text=value[@"title"];
- dataLb.text=value[@"time"];
- }
- }
- -(void)layoutSubviews{
- [super layoutSubviews];
- CGFloat width=self.contentView.bounds.size.width;
- CGFloat height=self.contentView.bounds.size.height*0.5f;
- CGFloat titlex=Kpadding;
- CGFloat titley=Kpadding;
- CGFloat titlew=width-*Kpadding;
- CGFloat titleh=height;
- titleLb.frame=CGRectMake(titlex, titley, titlew, titleh);
- CGFloat dx=titlex;
- CGFloat dy=titley+titleh;
- CGFloat dw=titlew;
- CGFloat dh=height-Kpadding;
- dataLb.frame=CGRectMake(dx, dy, dw, dh);
- }
- @end
地址 http://pan.baidu.com/share/link?shareid=1603134617&uk=923776187
ios中封装网络和tableview的综合运用的更多相关文章
- ios中封装网络请求类
#import "JSNetWork.h" //asiHttpRequest #import "ASIFormDataRequest.h" //xml 的解析 ...
- IOS中的网络编程
在移动互联网时代,几乎所有应用都需要用到网络下载,比如图片的加载,音乐的下载,安装包的下载,等等,下面我们来看看如何进行下载 一.文件的下载我们用get来请求数据,并对请求的二进制数据进行解析存入文件 ...
- IOS中封装一个View的思路
一.封装一个View的思路 1.将View内部的业务逻辑(显示内容)封装到View中 2.一般情况下,View的位置应该由父控件来决定,也就是位置不应该固定死在View内部 3.至于View的宽高,根 ...
- iOS中的网络请求 和 网络监测
1.网络监测 //根据主机名判断网络是否连接 Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu ...
- 聊聊 iOS 中的网络加密
介绍下 公司的接口一般会两种协议的,一种HTTP,一种HTTPS的,HTTP 只要请求,服务器就会响应,如果我们不对请求和响应做出加密处理,所有信息都是会被检测劫持到的,是很不安全的,客户端加密可以使 ...
- IOS中的网络编程详解
在移动互联网时代,几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力,如果没有了网络,也就缺少了数据变化,无论外观多 ...
- ios 中scrollview上面嵌套tableView,左右滑动出现数据多次刷新的问题
注意scrollView左右滑动时不要刷新数据,刚进来时一次性请求所有数据 红包纪录和房源信息可以左右滑动和点击,tableView可以上下滑动,图片部分个人信息只刷新一次. 界面布局如下
- iOS中 WGAFN_网络监控 技术分享
需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...
- ios中封装九宫格的使用(二级导航)
效果图 一般用于导航功能 第一步下载http://pan.baidu.com/share/link?shareid=1824940819&uk=923776187 第二步 把下图内容放在你的x ...
随机推荐
- Logistic Regression总结
转自:http://blog.csdn.net/dongtingzhizi/article/details/15962797 Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (20 ...
- LeetCode 总结
LeetCode 方法详解
- VS Code .vue文件代码缩进以及格式化代码
首先在应用商店中搜索“Vetur”插件安装,然后进行下面操作: 文件->首选项->设置,然后在右边编辑框输入以下设置: { "prettier.tabWidth": 4 ...
- JAVA-找不到元素 'beans' 的声明
问题: Tomcat启动时,spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseExceptio ...
- Windows远程桌面连接的利器-mRemote
mRemoteNG是Windows平台下一款开源的支持多标签.多协议的远程连接管理器.平时我们可能安装N多款管理工具,如putty.SecureCRT.xshell.SSHshell.mstsc.ex ...
- Ubuntu16.04中Docker的卸载
1:利用sudo apt-get remove docker 进行卸载提示如下,docker未按照所以不能卸载 2:再次查看docker版本,依然还在 3:原因是安装的时候安装的是docker-ce版 ...
- Sqlserver 2008 R2安装的盘符空间不够用的解决办法
例如我把一个sqlserver数据库安装在了D盘,结果发现D盘只剩下20G的可用空间,可是数据却每天的在增长,如何办?于是百度到了以下解决办法 方法很多: 1.可以给primary文件组添加文件.选择 ...
- ps叠加模式笔记
1.叠加模式:Overlay混色模式会让图层白色的部分去加亮底色,图层深色的部分去加暗底色 2.常见按钮:1)主体渐变:2)投影:3)内阴影:颜色减淡,按钮颜色,距离1,大小3:4)外发光,正片叠底,
- oauth2-server-php-docs 授权控制器
授权控制器 概观 对于授权端点,要求用户使用authorization code(授权码授权类型)或access token(隐式授权类型)对客户端进行认证和重定向. 方法 handleAuthori ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...