接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入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. UGUI技术之LayoutGroup布局实现详解

    继续学习unity4.6,unity的LayoutGroup分为三种, Horizontal Layout Group(水平布局)Vertical Layout Group(垂直布局)Grid Lay ...

  2. Java 时区(转)

    http://blog.csdn.net/wangpeng047/article/details/8560690

  3. JAVA团队开发手册 - 3. 开发流程

    开发流程 对于一个项目,最大的问题就是如何拆解为任务,分配到合适的人手里,并在有限的时间内完成它. 就像做建筑工程一样,其实做IT也是可以量化的,可能有的人砌砖砌得慢一些,有的人快一些. 但是我们把整 ...

  4. B - Halloween Costumes

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  5. socketserver 入门练习

    个人理解: 个人感觉socketserver其实就是为服务端专门提供的一个用于解决多用户并发访问需求的一个模块 小试牛刀: 服务端socketserver_server.py import socke ...

  6. CSS浮动float父div没有高度的问题

    如下所示,子元素 div2 本身具有高度和宽度,但由于其具有float:left:属性后.其父元素 div1 不具有高度. <html>    <head>    </h ...

  7. 使用openssl 生成免费证书

    阅读目录 一:什么是openssl? 它的作用是?应用场景是什么? 二:使用openssl生成免费证书 回到顶部 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openssl ...

  8. datetimepicker 插件位置问题解决经验

    使用dadetimepicker进行时间选择是个很不错的选择,但是美中不足的是该插件在chrome中显示弹框的时候有时会出现位置错位的现象,而在IE中则没有这种现象,视图如图1 图1 查阅了网上的资料 ...

  9. ubuntu下sublime屏蔽alt键显示顶部菜单栏

    在sulime下喜欢把方向键映的上下左右映射为alt+i/k/j/l ,按单词移动alt+h/; 但是在ubuntu下按alt会弹出sublime的顶部菜单栏,如果想屏蔽sublime按alt屏蔽菜单 ...

  10. eureka集群环境搭建

    一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eurek ...