zoukankan      html  css  js  c++  java
  • ios 团购信息客户端demo(三)

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

     

    [plain] view plaincopy
    1. #import <UIKit/UIKit.h>  
    2. #import "ASIHTTPRequest.h"  
    3. #import "AQGridView.h"  
    4.   
    5.   
    6. @interface KKFirstViewController : UIViewController<ASIHTTPRequestDelegate, AQGridViewDelegate, AQGridViewDataSource>  
    7.   
    8. @property(nonatomic, retain)AQGridView *gridView;  
    9.   
    10. @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.   
    7. [self.view addSubview:gridView];  

    将当前的gridView加入主视图中

     

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

     

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

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

     

     

    [plain] view plaincopy
    1. #import "AQGridViewCell.h"  
    2.   
    3. @interface GridViewCell : AQGridViewCell  
    4.   
    5. @property(nonatomic, retain)UIImageView *imageView;  
    6. @property(nonatomic, retain)UILabel *captionLabel;  
    7.   
    8. @end  

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

     

     

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

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

     

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

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

     

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

     

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

     

    [plain] view plaincopy
    1. //利用缓存保存图片  
  • 相关阅读:
    字符串习题小结
    字符串处理指令以及控制台输入
    初次接触JAVA有关重点
    常用的正则表达式
    JS里日历的两种写法
    win10 系统连不上打印机 操作无法完成(错误Ox00000709) 台式机无线网卡 设置固定IP 之后 IP变了
    win10 visual studio2019 目标框架选不到.net 4.8
    JsonPath 简单入门 与 xpath
    IIS 搭建HTTPS站点
    java mave 打包问题 发布找不到驱动类
  • 原文地址:https://www.cnblogs.com/yulang314/p/3549640.html
Copyright © 2011-2022 走看看