ios 团购信息客户端demo(三)
接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接可以引用
- #import <UIKit/UIKit.h>
- #import "ASIHTTPRequest.h"
- #import "AQGridView.h"
- @interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate, AQGridViewDelegate, AQGridViewDataSource>
- @property(nonatomic, retain)AQGridView *gridView;
- @end
这里加入了AQGridViewDelegate和AQGridViewDataSource这两个委托,简单一点我们可以把AQGridView看成UITableView,同样的道理,一个是数据源的方法,一个就是选中的方法
然后就是
在-(void)viewDidLoad这个方法中,我们加入了
- self.gridView = [[AQGridView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
- self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- self.gridView.autoresizesSubviews = YES;
- self.gridView.delegate = self;
- self.gridView.dataSource = self;
- [self.view addSubview:gridView];
将当前的gridView加入主视图中
接着还有两个方法一定需要实现的
- #pragma mark AQGridViewDataSource
- //总共有的Item
- -(NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView{
- return [arrays count];
- }
- //每个Item
- -(AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index{
- static NSString *identifier = @"PlainCell";
- GridViewCell *cell = (GridViewCell *)[aGridView dequeueReusableCellWithIdentifier:identifier];
- if(cell == nil){
- cell = [[GridViewCell alloc] initWithFrame:CGRectMake(0, 0, 160, 123) reuseIdentifier:identifier];
- }
- //取得每一个字典
- NSDictionary *dict = [arrays objectAtIndex:index];
- [cell.captionLabel setText:[dict objectForKey:kName_Title]];
- return cell;
- }
- //每个显示框大小
- -(CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView{
- return CGSizeMake(160, 123);
- }
这里还少一个类,就是GridView,这个类继承了AQGridViewCell,里面就是我们单独要显示的一个Item
- #import "AQGridViewCell.h"
- @interface GridViewCell : AQGridViewCell
- @property(nonatomic, retain)UIImageView *imageView;
- @property(nonatomic, retain)UILabel *captionLabel;
- @end
图片显示的是团购信息中的图片,还有一个是文本
- #import "GridViewCell.h"
- @implementation GridViewCell
- @synthesize imageView,captionLabel;
- - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
- if (self) {
- UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 123)];
- [mainView setBackgroundColor:[UIColor clearColor]];
- UIImageView *frameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 4, 142, 117)];
- [frameImageView setImage:[UIImage imageNamed:@"tab-mask.png"]];
- self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(13, 8, 135, 84)];
- self.captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 92, 127, 21)];
- [captionLabel setFont:[UIFont systemFontOfSize:14]];
- [mainView addSubview:imageView];
- [mainView addSubview:frameImageView];
- [mainView addSubview:captionLabel];
- [self.contentView addSubview:mainView];
- }
- return self;
- }
- @end
这里面定义了三个控件,两个控件是我们要传入的数据,一个图片,一个文本,还有一个就是我们单独Item的背景
做完这一些,运行一下,我们就可以看到有文字信息的效果了,但还没有加入图片显示功能,从这里我们就要考虑了,图片是我们划动的时候再加载呢还是一次性加载呢,考虑到效果和数据流量,我们还是用异步来加载数据,这就需要加入缓存的功能了,我们用一个NSMutableArray来实现缓存。
看一下代码呢,这代码也是参考了别人写的
- //缓存图片
- -(UIImage *)cachedImageForUrl:(NSURL *)url{
- id cacheObject = [self.cachedImage objectForKey:url];
- if (cacheObject == nil) {
- //添加占位符
- [self.cachedImage setObject:@"Loading..." forKey:url];
- ASIHTTPRequest *picRequest = [ASIHTTPRequest requestWithURL:url];
- picRequest.delegate = self;
- picRequest.didFinishSelector = @selector(didFinishRequestImage:);
- picRequest.didFailSelector = @selector(didFailRequestImage:);
- //加入队列
- [self.queue addOperation:picRequest];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- }else if(![cacheObject isKindOfClass:[UIImage class]]){
- cacheObject = nil;
- }
- return cacheObject;
- }
- //完成图片下载,并加入缓存
- -(void)didFinishRequestImage:(ASIHTTPRequest *)request{
- NSData *imageData = [request responseData];
- UIImage *image = [UIImage imageWithData:imageData];
- if (image != nil) {
- [self.cachedImage setObject:image forKey:request.url];
- [self.gridView reloadData];
- }
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- //下载失败
- -(void)didFailRequestImage:(ASIHTTPRequest *)request{
- NSLog(@"Error download Image %@", [request error]);
- //从当前缓存中移除
- [self.cachedImage removeObjectForKey:request.url];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
最后我们在Cell中加入显示图片的代码就可以了,就实现了异步加载图片
- //利用缓存保存图片
ios 团购信息客户端demo(三)的更多相关文章
- ios 团购信息客户端demo(一)
团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...
- ios 团购信息客户端demo(二)
接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...
- iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...
- ecshop首页调用团购信息产品购买人数
当我们在ecshop后台录入ecshop的团购信息之后,我们在ecshop的前台首页index.php里面,可以看到他能调用ecshop的团购商品信息,其中就包括团购商品的一些价格信息.但是如何在ec ...
- ios 团购分类页面(9宫格)
=-= 命名有些错误,但功能实现,以后注意下命名规范 WJViewGroup.h #import <UIKit/UIKit.h> @interface WJViewGroup : UIVi ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...
- iOS UI基础-9.1 UITableView 团购
概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图 团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...
- IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)
******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...
随机推荐
- 洛谷P2473 [SCOI2008]奖励关(期望+状压)
传送门 我数学期望还是太差了…… 先考虑状压模型,设$dp[i][S]$表示第$i$轮,当前宝物状态为$S$,能获得的最大期望分数 然而这个模型有一个问题,第$i$轮不一定能达到状态$S$ 那么考虑转 ...
- linux 搭建unixODBC ,并对接 PostgreSQL 9.3.4
环境:suse 11 ,64位的操作系统 unixODBC 版本:2.3.2 PostgreSQL 9.3.4 1 编译安装 unixODBC 下载 unixODBC :http://www.unix ...
- Oracle / PLSQL写语句 常用的几个函数
下面开始记录一下,自己在Oracle或者PLSQL常用的几个函数, 1add_months 增加或减去月份2. last_day(sysdate) 返回日期的最后一天3. months_between ...
- linux 设置固定IP centOS6.5
主要是要把Linux的IP固定下来,可以用另一台机器SSH连接. ping的用法: 基本语法:ping [-选项] IP地址或域名 功能描述:测试网络是否连通 常用选项:-c -c 指定发送数据包的次 ...
- jQuery toggleClass 源码解读
toggleClass: function( value, stateVal ) { var type = typeof value;//值类型 if ( typeof stateVal === &q ...
- Unity Shader入门精要学习笔记 - 第2章 渲染流水线
来源作者:candycat http://blog.csdn.net/candycat1992/article/ 2.1 综述 渲染流水线的最终目的在于生成或者说是渲染一张二维纹理,即我们在电脑屏 ...
- ASP.NET页面传值的方法
ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...
- asp.net网页跳转
1.Response.Redirect("http://www.bcbbs.net",false); 目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool ...
- Spring使用_进阶
概述 本文主要写了几个关于Spring Aware,多线程,计划任务(定时任务),条件注解,组合注解,元注解,Spring测试的小例子以及关于@Enable*注解的工作原理的理解. Spring Aw ...
- zuul filter
前言 过滤器是Zuul的核心组件,这篇文章我们来详细讨论Zuul的过滤器.下面话不多说,来看看详细的介绍吧. 过滤器类型与请求生命周期 Zuul大部分功能都是通过过滤器来实现的.Zuul中定义了四种标 ...