接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接可以引用

[plain] view plaincopy

  1. #import <UIKit/UIKit.h>
  2. #import "ASIHTTPRequest.h"
  3. #import "AQGridView.h"
  4. @interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate, AQGridViewDelegate, AQGridViewDataSource>
  5. @property(nonatomic, retain)AQGridView *gridView;
  6. @end

这里加入了AQGridViewDelegate和AQGridViewDataSource这两个委托,简单一点我们可以把AQGridView看成UITableView,同样的道理,一个是数据源的方法,一个就是选中的方法

然后就是
在-(void)viewDidLoad这个方法中,我们加入了

[plain] view plaincopy

  1. self.gridView = [[AQGridView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
  2. self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  3. self.gridView.autoresizesSubviews = YES;
  4. self.gridView.delegate = self;
  5. self.gridView.dataSource = self;
  6. [self.view addSubview:gridView];

将当前的gridView加入主视图中

接着还有两个方法一定需要实现的

[plain] view plaincopy

  1. #pragma mark AQGridViewDataSource
  2. //总共有的Item
  3. -(NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView{
  4. return [arrays count];
  5. }
  6. //每个Item
  7. -(AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index{
  8. static NSString *identifier = @"PlainCell";
  9. GridViewCell *cell = (GridViewCell *)[aGridView dequeueReusableCellWithIdentifier:identifier];
  10. if(cell == nil){
  11. cell = [[GridViewCell alloc] initWithFrame:CGRectMake(0, 0, 160, 123) reuseIdentifier:identifier];
  12. }
  13. //取得每一个字典
  14. NSDictionary *dict = [arrays objectAtIndex:index];
  15. [cell.captionLabel setText:[dict objectForKey:kName_Title]];
  16. return cell;
  17. }
  18. //每个显示框大小
  19. -(CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView{
  20. return CGSizeMake(160, 123);
  21. }

这里还少一个类,就是GridView,这个类继承了AQGridViewCell,里面就是我们单独要显示的一个Item

[plain] view plaincopy

  1. #import "AQGridViewCell.h"
  2. @interface GridViewCell : AQGridViewCell
  3. @property(nonatomic, retain)UIImageView *imageView;
  4. @property(nonatomic, retain)UILabel *captionLabel;
  5. @end

图片显示的是团购信息中的图片,还有一个是文本

[plain] view plaincopy

  1. #import "GridViewCell.h"
  2. @implementation GridViewCell
  3. @synthesize imageView,captionLabel;
  4. - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
  5. {
  6. self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
  7. if (self) {
  8. UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 123)];
  9. [mainView setBackgroundColor:[UIColor clearColor]];
  10. UIImageView *frameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 4, 142, 117)];
  11. [frameImageView setImage:[UIImage imageNamed:@"tab-mask.png"]];
  12. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(13, 8, 135, 84)];
  13. self.captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 92, 127, 21)];
  14. [captionLabel setFont:[UIFont systemFontOfSize:14]];
  15. [mainView addSubview:imageView];
  16. [mainView addSubview:frameImageView];
  17. [mainView addSubview:captionLabel];
  18. [self.contentView addSubview:mainView];
  19. }
  20. return self;
  21. }
  22. @end

这里面定义了三个控件,两个控件是我们要传入的数据,一个图片,一个文本,还有一个就是我们单独Item的背景

做完这一些,运行一下,我们就可以看到有文字信息的效果了,但还没有加入图片显示功能,从这里我们就要考虑了,图片是我们划动的时候再加载呢还是一次性加载呢,考虑到效果和数据流量,我们还是用异步来加载数据,这就需要加入缓存的功能了,我们用一个NSMutableArray来实现缓存。

看一下代码呢,这代码也是参考了别人写的

[plain] view plaincopy

  1. //缓存图片
  2. -(UIImage *)cachedImageForUrl:(NSURL *)url{
  3. id cacheObject = [self.cachedImage objectForKey:url];
  4. if (cacheObject == nil) {
  5. //添加占位符
  6. [self.cachedImage setObject:@"Loading..." forKey:url];
  7. ASIHTTPRequest *picRequest = [ASIHTTPRequest requestWithURL:url];
  8. picRequest.delegate = self;
  9. picRequest.didFinishSelector = @selector(didFinishRequestImage:);
  10. picRequest.didFailSelector = @selector(didFailRequestImage:);
  11. //加入队列
  12. [self.queue addOperation:picRequest];
  13. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  14. }else if(![cacheObject isKindOfClass:[UIImage class]]){
  15. cacheObject = nil;
  16. }
  17. return cacheObject;
  18. }
  19. //完成图片下载,并加入缓存
  20. -(void)didFinishRequestImage:(ASIHTTPRequest *)request{
  21. NSData *imageData = [request responseData];
  22. UIImage *image = [UIImage imageWithData:imageData];
  23. if (image != nil) {
  24. [self.cachedImage setObject:image forKey:request.url];
  25. [self.gridView reloadData];
  26. }
  27. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  28. }
  29. //下载失败
  30. -(void)didFailRequestImage:(ASIHTTPRequest *)request{
  31. NSLog(@"Error download Image %@", [request error]);
  32. //从当前缓存中移除
  33. [self.cachedImage removeObjectForKey:request.url];
  34. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  35. }

最后我们在Cell中加入显示图片的代码就可以了,就实现了异步加载图片

[plain] view plaincopy

  1. //利用缓存保存图片

ios 团购信息客户端demo(三)的更多相关文章

  1. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...

  2. ios 团购信息客户端demo(二)

    接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...

  3. iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)

    大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...

  4. ecshop首页调用团购信息产品购买人数

    当我们在ecshop后台录入ecshop的团购信息之后,我们在ecshop的前台首页index.php里面,可以看到他能调用ecshop的团购商品信息,其中就包括团购商品的一些价格信息.但是如何在ec ...

  5. ios 团购分类页面(9宫格)

    =-= 命名有些错误,但功能实现,以后注意下命名规范 WJViewGroup.h #import <UIKit/UIKit.h> @interface WJViewGroup : UIVi ...

  6. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  7. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例

    一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...

  8. iOS UI基础-9.1 UITableView 团购

    概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图      团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...

  9. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

随机推荐

  1. 洛谷P2473 [SCOI2008]奖励关(期望+状压)

    传送门 我数学期望还是太差了…… 先考虑状压模型,设$dp[i][S]$表示第$i$轮,当前宝物状态为$S$,能获得的最大期望分数 然而这个模型有一个问题,第$i$轮不一定能达到状态$S$ 那么考虑转 ...

  2. linux 搭建unixODBC ,并对接 PostgreSQL 9.3.4

    环境:suse 11 ,64位的操作系统 unixODBC 版本:2.3.2 PostgreSQL 9.3.4 1 编译安装 unixODBC 下载 unixODBC :http://www.unix ...

  3. Oracle / PLSQL写语句 常用的几个函数

    下面开始记录一下,自己在Oracle或者PLSQL常用的几个函数, 1add_months 增加或减去月份2. last_day(sysdate) 返回日期的最后一天3. months_between ...

  4. linux 设置固定IP centOS6.5

    主要是要把Linux的IP固定下来,可以用另一台机器SSH连接. ping的用法: 基本语法:ping [-选项] IP地址或域名 功能描述:测试网络是否连通 常用选项:-c -c 指定发送数据包的次 ...

  5. jQuery toggleClass 源码解读

    toggleClass: function( value, stateVal ) { var type = typeof value;//值类型 if ( typeof stateVal === &q ...

  6. Unity Shader入门精要学习笔记 - 第2章 渲染流水线

    来源作者:candycat   http://blog.csdn.net/candycat1992/article/ 2.1 综述 渲染流水线的最终目的在于生成或者说是渲染一张二维纹理,即我们在电脑屏 ...

  7. ASP.NET页面传值的方法

    ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...

  8. asp.net网页跳转

    1.Response.Redirect("http://www.bcbbs.net",false);    目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool ...

  9. Spring使用_进阶

    概述 本文主要写了几个关于Spring Aware,多线程,计划任务(定时任务),条件注解,组合注解,元注解,Spring测试的小例子以及关于@Enable*注解的工作原理的理解. Spring Aw ...

  10. zuul filter

    前言 过滤器是Zuul的核心组件,这篇文章我们来详细讨论Zuul的过滤器.下面话不多说,来看看详细的介绍吧. 过滤器类型与请求生命周期 Zuul大部分功能都是通过过滤器来实现的.Zuul中定义了四种标 ...