1:封装网络请求 类

  1. #import <Foundation/Foundation.h>
  2. #import "ASIFormDataRequest.h"
  3. #import "Reachability.h"
  4.  
  5. @protocol NetWorkDelegate;
  6.  
  7. @interface JSNetWord : NSObject<ASIHTTPRequestDelegate>
  8. +(id)ShareNetwork;
  9. -(void)NetWorkWithConnctId:(int)connectid body:(NSString *)body PostBody:(NSString *)PostBody aDelegate:(id<NetWorkDelegate>)delegate;
  10. @end
  11.  
  12. @protocol NetWorkDelegate <NSObject>
  13.  
  14. -(void)NetWorkWithConnectId:(int)connectid aBackString:(NSString *)backString withState:(int) state;
  15.  
  16. @end
  17.  
  18. #import "JSNetWord.h"
  19.  
  20. @implementation JSNetWord
  21. +(id)ShareNetwork{
  22. static dispatch_once_t onceToken;
  23. static JSNetWord *netWork=nil;
  24. dispatch_once(&onceToken, ^{
  25. netWork=[[JSNetWord alloc] init];
  26. });
  27. return netWork;
  28. }
  29.  
  30. //网络请求
  31. -(void)NetWorkWithConnctId:(int)connectid body:(NSString *)body PostBody:(NSString *)PostBody aDelegate:(id<NetWorkDelegate>)delegate{
  32. if([self IsConnect]){
  33. UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:nil message:@"请联网" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
  34. [alertview show];
  35. [alertview release];
  36. if(delegate && [delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:withState:)]){
  37. [delegate NetWorkWithConnectId:connectid aBackString:nil withState:];
  38. }
  39. return;
  40. }
  41. ASIFormDataRequest *Request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://61.177.61.252/szzssw/taxCommon.action?code=1001&pagecode=1&menucode=3300000000"]];
  42. Request.timeOutSeconds=;
  43. Request.requestMethod=@"POST";
  44. Request.delegate=self;
  45. NSArray *PostBodyArrary=[PostBody componentsSeparatedByString:@"&"];
  46. for(NSString *temp in PostBodyArrary){
  47. NSArray *postArray=[temp componentsSeparatedByString:@"="];
  48. [Request setPostValue:postArray[] forKey:postArray[]];
  49. }
  50. //封装参数
  51. NSString *cid=[NSString stringWithFormat:@"%zi",connectid];
  52. Request.userInfo=@{@"cid":cid,@"delegate":delegate};
  53. [Request startAsynchronous];
  54. }
  55.  
  56. #pragma mark -ASIHttpRequest delegate
  57. -(void)requestStarted:(ASIHTTPRequest *)request{
  58. //可以加入 toast
  59. }
  60.  
  61. -(void)requestFinished:(ASIHTTPRequest *)request{
  62. NSDictionary *dic=request.userInfo;
  63. int connectid= [dic[@"cid"] intValue];
  64. id<NetWorkDelegate> delegate=dic[@"delegate"];
  65. if (delegate &&[delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:withState:)]) {
  66. [delegate NetWorkWithConnectId:connectid aBackString:[ request responseString] withState:];
  67. }
  68.  
  69. }
  70.  
  71. -(void)requestFailed:(ASIHTTPRequest *)request{
  72. NSDictionary *dic=request.userInfo;
  73. int connectid= [dic[@"cid"] intValue];
  74. id<NetWorkDelegate> delegate=dic[@"delegate"];
  75. NSLog(@"错误原因:-->%@",request.error.localizedDescription);
  76. if(delegate &&[delegate respondsToSelector:@selector(NetWorkWithConnectId:aBackString:)]){
  77. [delegate NetWorkWithConnectId:connectid aBackString:[request responseString] withState:];
  78. }
  79. UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请求失败" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
  80. [alertview show];
  81. [alertview release];
  82. }
  83.  
  84. //判断是否联网
  85. -(BOOL)IsConnect{
  86. BOOL IsCon=NO;
  87. Reachability *r=[Reachability reachabilityWithHostName:@"www.baidu.com"];
  88. switch ([r currentReachabilityStatus]) {
  89. case NotReachable:
  90. NSLog(@"not network");
  91. IsCon=NO;
  92. break;
  93. case ReachableViaWiFi:
  94. NSLog(@"wifi");
  95. IsCon=YES;
  96. break;
  97. case ReachableViaWWAN:
  98. NSLog(@"3g");
  99. IsCon=YES;
  100. break;
  101.  
  102. default:
  103. break;
  104. }
  105. return IsCon;
  106. }
  107.  
  108. @end

封装上下拉tableview .

配置步骤:英文原文和类库下载地址:https://github.com/emreberge/EGOTableViewPullRefresh

  • 添加 QuartzCore.framework 到你的工程中。
  • 将 EGOTableViewPullRefresh 拖到你的工程目录下。
  • 查看 PullTableView.h 文件可用的属性。
  • 添加一个PullTableView 到你代码中,实现PullTableViewDelegate委托方法
  1. #import <UIKit/UIKit.h>
  2. #import "PullTableView.h"
  3.  
  4. @interface JSContentView : UIView<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate>
  5.  
  6. -(id)initWithFrame:(CGRect)frame requestSEL:(SEL)aselector aDelegate:(id)delegate;
  7. @property(nonatomic,assign)int Pagecode;
  8. @property(nonatomic,readonly)PullTableView *tableview;
  9. @property(nonatomic,retain)NSMutableArray *mydata;
  10. @end
  11.  
  12. #import "JSContentView.h"
  13. #import "MyCell.h"
  14.  
  15. @interface JSContentView ()
  16. {
  17. SEL selector;
  18. id delegate;
  19. }
  20.  
  21. @end
  22.  
  23. @implementation JSContentView
  24.  
  25. #pragma mark -初始化
  26. -(id)initWithFrame:(CGRect)frame requestSEL:(SEL)aselector aDelegate:(id)adelegate{
  27. self=[super initWithFrame:frame];
  28. if(self){
  29. self.mydata=[NSMutableArray array];
  30. _tableview =[[PullTableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
  31. _tableview.delegate=self;
  32. _tableview.dataSource=self;
  33. _tableview.pullDelegate=self;
  34.  
  35. //设置pulltableview 的背景色
  36. _tableview.pullBackgroundColor=[UIColor clearColor];
  37. _tableview.pullTextColor=[UIColor blackColor];
  38.  
  39. [self addSubview:_tableview];
  40. selector=aselector;
  41. delegate=adelegate;
  42.  
  43. //第一次加载的时候默认要刷新。
  44. if(!self.tableview.pullTableIsRefreshing){
  45. self.tableview.pullTableIsRefreshing=YES;
  46. if (delegate&&[delegate respondsToSelector:selector]) {
  47. [delegate performSelector:selector];
  48. }
  49. }
  50. self.tableview.pullTableIsLoadingMore=NO;
  51. }
  52. return self;
  53. }
  54.  
  55. - (void)dealloc
  56. {
  57. [_mydata release];
  58. [super dealloc];
  59. }
  60.  
  61. #pragma mark -tableview dataSource
  62. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  63. return ;
  64. }
  65.  
  66. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  67. return self.mydata.count;
  68. }
  69.  
  70. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  71. static NSString *cellIdentify=@"myCell";
  72. MyCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
  73. if (cell==nil) {
  74. cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
  75. }
  76. NSDictionary *dic=self.mydata[indexPath.row];
  77. cell.value=dic;
  78. return cell;
  79. }
  80.  
  81. #pragma mark -tableview delegate
  82. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  83. return ;
  84. }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  1.  
  1.  

NSString *str = [NSStringstringWithFormat:@"%d",indexPath.row];

  1.  

if(self.delegate && [self.delegate respondsToSelector:@selector(EnterDetailPage:)]){

  1.  

[self.delegate performSelector:@selector(EnterDetailPage:) withObject:str];

  1.  

}

  1.  

}

  1.  

在viewcontroller里面运用起来。

  1. #import <UIKit/UIKit.h>
  2.  
  3. #import "JSContentView.h"
  4. #import "JSNetWord.h"
  5.  
  6. @interface ViewController : UIViewController<NetWorkDelegate>
  7.  
  8. @end
  9.  
  10. ===========
  11. #import "ViewController.h"
  12. #import "MyCell.h"
  13.  
  14. @interface ViewController ()
  15. {
  16. JSContentView *newView;
  17. }
  18.  
  19. @property(nonatomic,retain)NSMutableArray *dataArray;
  20. @end
  21.  
  22. @implementation ViewController
  23.  
  24. - (void)viewDidLoad
  25. {
  26. [super viewDidLoad];
  27. newView=[[JSContentView alloc] initWithFrame:self.view.bounds requestSEL:@selector(RequestData) aDelegate:self];
  28. [self.view addSubview:newView];
  29. [newView release];
  30. //如果开始启用缓存
  31. // NSString *cache=[self readApiCacheFileName:@"1.txt"];
  32. // if (cache.length>0 &&newView.Pagecode<=1) {
  33. // [self NetWorkWithConnectId:100 aBackString:cache withState:0];
  34. // }
  35.  
  36. }
  37.  
  38. -(void)RequestData{
  39. NSString *url=[NSString stringWithFormat:@"http://61.177.61.252/szzssw/taxCommon.action?code=1001&pagecode=%zi&menucode=3300000000",newView.Pagecode==?:newView.Pagecode];
  40. //请求数据,重新刷新数据
  41. [[JSNetWord ShareNetwork] NetWorkWithConnctId: body:url PostBody:nil aDelegate:self];
  42. //先从缓存中读取
  43. NSString *cache=[self readApiCacheFileName:@"1.txt"];
  44.  
  45. if (cache.length> &&newView.Pagecode==) {
  46. [self NetWorkWithConnectId: aBackString:cache withState:];
  47. }
  48.  
  49. }
  50.  
  51. #pragma mark -network delegate
  52. -(void)NetWorkWithConnectId:(int)connectid aBackString:(NSString *)backString withState:(int)state{
  53. if(connectid==){
    //1:代表网络 0代表缓存
  54. if (newView.Pagecode<= && state==) {
  55. newView.tableview.pullTableIsRefreshing=NO;
  56. }
  57. if (newView.Pagecode>) {
  58. newView.tableview.pullTableIsLoadingMore=NO;
  59. }
  60.  
  61. NSDictionary *dic=[backString JSONValue];
  62. NSString *dealcode=dic[@"dealcode"];
  63. if (![dealcode isEqualToString:@""]) {
  64. return;
  65. }
  66.  
  67. if (newView.Pagecode==) {
  68. [self saveApiCacheFileName:@"1.txt" textContent:backString Append:NO];
  69. }
  70. if (newView.Pagecode== &&newView.mydata.count>) {
  71. [newView.mydata removeAllObjects];
  72. }
  73. NSArray *data=dic[@"content"];
  74. [newView.mydata addObjectsFromArray:data];
  75. [newView.tableview reloadData];
  76.  
  77. }
  78. }

-(void)EnterDetailPage:(NSString *)row{

  1.  

int r=[row intValue];

  1.  

NSDictionary *dic=newView.mydata[r];

  1.  

NSLog(@"%@",dic[@"title"]);

  1.  

//可以创建指定controlview。

  1.  

//    ViewController *vc=[[ViewController alloc] init];

  1.  

//    ......

  1.  

}

  1. -(void)viewDidUnload{
  2. [super viewDidUnload];
  3. self.dataArray=nil;
  4. }
  5.  
  6. - (void)dealloc
  7. {
  8. [_dataArray release];
  9. [super dealloc];
  10. }
  11.  
  12. @end

其中自定义cell

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface MyCell : UITableViewCell
  4.  
  5. @property(nonatomic,retain)NSDictionary *value;
  6. @end
  7.  
  8. #import "MyCell.h"
  9. #define KTitleFont 15
  10. #define KFont 12
  11. #define Kpadding 10
  12.  
  13. @interface MyCell (){
  14. UILabel *titleLb;
  15. UILabel *Contetlb;
  16. UILabel *dataLb;
  17. }
  18.  
  19. @end
  20.  
  21. @implementation MyCell
  22.  
  23. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  24. {
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if (self) {
  27. titleLb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor redColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:KTitleFont]];
  28. // Contetlb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentRight font:[UIFont systemFontOfSize:KFont]];
  29. dataLb=[UILabel LabWithFrame:CGRectZero text:@"" textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:KFont]];
  30. [self.contentView addSubview:titleLb];
  31.  
  32. [self.contentView addSubview:dataLb];
  33. }
  34. return self;
  35. }
  36. -(void)setValue:(NSDictionary *)value{
  37. if(_value!=value){
  38. [_value release];
  39. _value=[value retain];
  40. titleLb.text=value[@"title"];
  41. dataLb.text=value[@"time"];
  42. }
  43. }
  44. -(void)layoutSubviews{
  45. [super layoutSubviews];
  46. CGFloat width=self.contentView.bounds.size.width;
  47. CGFloat height=self.contentView.bounds.size.height*0.5f;
  48. CGFloat titlex=Kpadding;
  49. CGFloat titley=Kpadding;
  50. CGFloat titlew=width-*Kpadding;
  51. CGFloat titleh=height;
  52. titleLb.frame=CGRectMake(titlex, titley, titlew, titleh);
  53.  
  54. CGFloat dx=titlex;
  55. CGFloat dy=titley+titleh;
  56. CGFloat dw=titlew;
  57. CGFloat dh=height-Kpadding;
  58. dataLb.frame=CGRectMake(dx, dy, dw, dh);
  59.  
  60. }
  61.  
  62. @end

地址 http://pan.baidu.com/share/link?shareid=1603134617&uk=923776187

ios中封装网络和tableview的综合运用的更多相关文章

  1. ios中封装网络请求类

    #import "JSNetWork.h" //asiHttpRequest #import "ASIFormDataRequest.h" //xml 的解析 ...

  2. IOS中的网络编程

    在移动互联网时代,几乎所有应用都需要用到网络下载,比如图片的加载,音乐的下载,安装包的下载,等等,下面我们来看看如何进行下载 一.文件的下载我们用get来请求数据,并对请求的二进制数据进行解析存入文件 ...

  3. IOS中封装一个View的思路

    一.封装一个View的思路 1.将View内部的业务逻辑(显示内容)封装到View中 2.一般情况下,View的位置应该由父控件来决定,也就是位置不应该固定死在View内部 3.至于View的宽高,根 ...

  4. iOS中的网络请求 和 网络监测

    1.网络监测 //根据主机名判断网络是否连接 Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu ...

  5. 聊聊 iOS 中的网络加密

    介绍下 公司的接口一般会两种协议的,一种HTTP,一种HTTPS的,HTTP 只要请求,服务器就会响应,如果我们不对请求和响应做出加密处理,所有信息都是会被检测劫持到的,是很不安全的,客户端加密可以使 ...

  6. IOS中的网络编程详解

    在移动互联网时代,几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力,如果没有了网络,也就缺少了数据变化,无论外观多 ...

  7. ios 中scrollview上面嵌套tableView,左右滑动出现数据多次刷新的问题

    注意scrollView左右滑动时不要刷新数据,刚进来时一次性请求所有数据 红包纪录和房源信息可以左右滑动和点击,tableView可以上下滑动,图片部分个人信息只刷新一次. 界面布局如下

  8. iOS中 WGAFN_网络监控 技术分享

    需要用到第三方AFNetworking/SVProgressHUD 没有的可以关注我微博私信我.http://weibo.com/hanjunqiang AppDelegate.m #import & ...

  9. ios中封装九宫格的使用(二级导航)

    效果图 一般用于导航功能 第一步下载http://pan.baidu.com/share/link?shareid=1824940819&uk=923776187 第二步 把下图内容放在你的x ...

随机推荐

  1. Logistic Regression总结

    转自:http://blog.csdn.net/dongtingzhizi/article/details/15962797 Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (20 ...

  2. LeetCode 总结

    LeetCode 方法详解

  3. VS Code .vue文件代码缩进以及格式化代码

    首先在应用商店中搜索“Vetur”插件安装,然后进行下面操作: 文件->首选项->设置,然后在右边编辑框输入以下设置: { "prettier.tabWidth": 4 ...

  4. JAVA-找不到元素 'beans' 的声明

    问题: Tomcat启动时,spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseExceptio ...

  5. Windows远程桌面连接的利器-mRemote

    mRemoteNG是Windows平台下一款开源的支持多标签.多协议的远程连接管理器.平时我们可能安装N多款管理工具,如putty.SecureCRT.xshell.SSHshell.mstsc.ex ...

  6. Ubuntu16.04中Docker的卸载

    1:利用sudo apt-get remove docker 进行卸载提示如下,docker未按照所以不能卸载 2:再次查看docker版本,依然还在 3:原因是安装的时候安装的是docker-ce版 ...

  7. Sqlserver 2008 R2安装的盘符空间不够用的解决办法

    例如我把一个sqlserver数据库安装在了D盘,结果发现D盘只剩下20G的可用空间,可是数据却每天的在增长,如何办?于是百度到了以下解决办法 方法很多: 1.可以给primary文件组添加文件.选择 ...

  8. ps叠加模式笔记

    1.叠加模式:Overlay混色模式会让图层白色的部分去加亮底色,图层深色的部分去加暗底色 2.常见按钮:1)主体渐变:2)投影:3)内阴影:颜色减淡,按钮颜色,距离1,大小3:4)外发光,正片叠底,

  9. oauth2-server-php-docs 授权控制器

    授权控制器 概观 对于授权端点,要求用户使用authorization code(授权码授权类型)或access token(隐式授权类型)对客户端进行认证和重定向. 方法 handleAuthori ...

  10. [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 ...