转载:

屎壳郎情调-成长日记

首先要知道:瀑布流的核心就是要获取到图片的长宽

网上的很多例子都是加载本地图片的 对于新手而言 改成加载网络图片的确是有点压力的  因为本地的图片 我们是很容易就能获取到他的 长宽的 但是网络图片的话 我们只能是先加载图片 然后得到他的长宽 这个时候就涉及到 异步加载图片的问题了  我们现在的思路是 获取网络图片地址 然后 开辟线程来加载该地址的图片 从而获取他的长宽

 #import "shopRViewController.h"
#import "TMQuiltView.h" #import "TMPhotoQuiltViewCell.h" @interface shopRViewController ()<TMQuiltViewDataSource,TMQuiltViewDelegate>
{
TMQuiltView *qtmquitView;
}
@property (nonatomic, retain) NSMutableArray *images;
@end @implementation shopRViewController @synthesize images = _images; - (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0.2]];
//请求数据
self.factory = [[DataFactory alloc] initDataWithUrlString:ChanPinTuiJianHttpUrl vHttpMethod:@"post"];
self.factory.delegate = self;
qtmquitView = [[TMQuiltView alloc] initWithFrame:CGRectMake(, , , )];
qtmquitView.delegate = self;
qtmquitView.dataSource = self; [self.view addSubview:qtmquitView]; // [self createHeaderView];
// [self performSelector:@selector(testFinishedLoadData) withObject:nil afterDelay:0.0f]; }
//解析数据
-(void)JsonDataDictionaryDetail:(NSMutableDictionary *)dataDic
{
self.arrayHeight = [[NSMutableArray alloc] initWithCapacity:];
self.arrayWidth = [[NSMutableArray alloc] initWithCapacity:];
@try {
if(dataDic!=nil)
{
if(self.arrayProductDetail==nil)
{
self.arrayProductDetail = [[NSMutableArray alloc] initWithCapacity:]; }
else
{
[self.arrayProductDetail removeAllObjects];
[self.arrCopy removeAllObjects];
}
for (int i = ; i<[[dataDic objectForKey:@"data"] count]; i++) {
ProdectDetail *p = [[ProdectDetail alloc] init];
p.ID = [dataDic objectForKey:@"data"][i][@"id"];
p.img = [dataDic objectForKey:@"data"][i][@"img"];
p.title = [dataDic objectForKey:@"data"][i][@"title"];
p.marketprice = [NSString stringWithFormat:@"%.2f",[[dataDic objectForKey:@"data"][i][@"marketprice"] floatValue]];
p.sellprice =[NSString stringWithFormat:@"%.2f",[[dataDic objectForKey:@"data"][i][@"sellprice"] floatValue]];
p.guige = [dataDic objectForKey:@"data"][i][@"guige"];
p.hots = [dataDic objectForKey:@"data"][i][@"hots"];
[self.arrayProductDetail addObject:p];
//开辟线程来加载图片
[self performSelectorInBackground:@selector(backgr:) withObject:p];
}
self.arrCopy = [NSMutableArray arrayWithArray:self.arrayProductDetail]; }
else
{
[self Msg:@"当前无网络连接!"];
} }
@catch (NSException *exception) { } [qtmquitView reloadData];
}
//线程加载图片获取 长 宽
-(void)backgr:(ProdectDetail *)p
{
NSURL *url = [NSURL URLWithString:p.img];
UIImage *imga = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
[self.arrayWidth addObject:[NSString stringWithFormat:@"%f",imga.size.width]];
[self.arrayHeight addObject:[NSString stringWithFormat:@"%f",imga.size.height]];
//更新主线程
[self performSelectorOnMainThread:@selector(updateMain) withObject:nil waitUntilDone:NO];
}
//刷新主线程做的事情
-(void)updateMain
{
//主线层要做的事情就是刷新布局
//刷新布局
[qtmquitView reloadData];
} //cell的个数
- (NSInteger)quiltViewNumberOfCells:(TMQuiltView *)TMQuiltView {
return self.arrayWidth.count;//注意 这个count要跟 子线程里面的组数个数一致 (之前越界了 原来问题处在这里)
}
//cell事件
- (TMQuiltViewCell *)quiltView:(TMQuiltView *)quiltView cellAtIndexPath:(NSIndexPath *)indexPath {
TMPhotoQuiltViewCell *cell = (TMPhotoQuiltViewCell *)[quiltView dequeueReusableCellWithReuseIdentifier:@"PhotoCell"];
if (!cell) {
cell = [[[TMPhotoQuiltViewCell alloc] initWithReuseIdentifier:@"PhotoCell"] autorelease];
}
ProdectDetail *p = [self.arrayProductDetail objectAtIndex:indexPath.row];
// cell.photoView.image = [self imageAtIndexPath:indexPath];
[cell.photoView setImageWithURL:[NSURL URLWithString:p.img]];
[cell.titleLabel setFont:[UIFont systemFontOfSize:]];
[cell.headLabel setTextColor:[UIColor orangeColor]];
cell.headLabel.text = [NSString stringWithFormat:@"¥ %@",p.sellprice];
cell.titleLabel.text = [NSString stringWithFormat:@"%@%d",p.title, indexPath.row];
return cell;
} #pragma mark - TMQuiltViewDelegate
//设备旋转
- (NSInteger)quiltViewNumberOfColumns:(TMQuiltView *)quiltView { if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft
|| [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
return ;
} else {
return ;
}
}
//cell高度 这个是关键点
- (CGFloat)quiltView:(TMQuiltView *)quiltView heightForCellAtIndexPath:(NSIndexPath *)indexPath
{ if(self.arrayHeight.count>)
{
float cell_height = 0.9*viewWidth/*[[self.arrayHeight objectAtIndex:indexPath.row] floatValue]/[[self.arrayWidth objectAtIndex:indexPath.row] floatValue]; return cell_height;
}
}
//选中事件要做的事情
- (void)quiltView:(TMQuiltView *)quiltView didSelectCellAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"index:%d",indexPath.row);
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

demo使用注意点:


使用方法

在TMQuiltView.m里面 修改间距 

constCGFloat kTMQuiltViewDefaultMargin = 5.0f;      //修改间距

修改Cell的布局样式 在 TMPhotoQuiltViewCell.m里面 添加布局样式

添加布局 

/

#import "TMPhotoQuiltViewCell.h"

constCGFloat kTMPhotoQuiltViewMargin = ;

@implementation TMPhotoQuiltViewCell

@synthesize photoView =_photoView;

@synthesize titleLabel =_titleLabel;

- (void)dealloc {

    [_photoView release], _photoView =nil;

    [_titleLabel release], _titleLabel =nil;

    [superdealloc];

}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier

{

   self = [superinitWithReuseIdentifier:reuseIdentifier];

   if (self) {

        self.backgroundColor = [UIColorwhiteColor];

    }

    return self;

}

- (UIImageView *)photoView {

   if (!_photoView) {

       _photoView = [[UIImageViewalloc] init];

        _photoView.contentMode =UIViewContentModeScaleAspectFill;

       _photoView.clipsToBounds =YES;

        [selfaddSubview:_photoView];

    }

    return_photoView;

}

//

//注意 添加的的布局一定要你写成这样 get set 形式否则会闪屏  

//

//

- (UILabel *)titleLabel {

    if (!_titleLabel) {

       _titleLabel = [[UILabelalloc] init];

        _titleLabel.backgroundColor = [[UIColorblackColor] colorWithAlphaComponent:0.5];

       _titleLabel.textColor = [UIColorwhiteColor];

        _titleLabel.textAlignment =UITextAlignmentCenter;

        [selfaddSubview:_titleLabel];

    }

    return_titleLabel;

}

//布局

- (void)layoutSubviews {

    self.photoView.frame =CGRectInset(self.bounds,kTMPhotoQuiltViewMargin, kTMPhotoQuiltViewMargin);

    //下标题

    self.titleLabel.frame =CGRectMake(kTMPhotoQuiltViewMargin,self.bounds.size.height -  - kTMPhotoQuiltViewMargin,

                                      self.bounds.size.width - * kTMPhotoQuiltViewMargin,);

}

@end

demo下载地址 (加载本地图片的)

http://download.csdn.net/detail/aa741649143/6518895

ios 瀑布流的那些事情的更多相关文章

  1. IOS 瀑布流UICollectionView实现

    IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...

  2. iOS 瀑布流之栅格布局

    代码地址如下:http://www.demodashi.com/demo/14760.html 一 .效果预览 二.确定需求 由下面的需求示意图可知模块的最小单位是正方形,边长是屏幕宽除去边距间隔后的 ...

  3. iOS 瀑布流封装

    代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...

  4. IOS 瀑布流

    本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...

  5. iOS瀑布流实现(Swift)

    这段时间突然想到一个很久之前用到的知识-瀑布流,本来想用一个简单的方法,发现自己走入了歧途,最终只能狠下心来重写UICollectionViewFlowLayout.下面我将用两种方法实现瀑布流,以及 ...

  6. iOS 瀑布流的Demo

    /** * 瀑布流Demo的主要代码,若想看完整的代码请到下面链接去下载 * * 链接: https://pan.baidu.com/s/1slByAHB 密码: r3q6 */ #import &l ...

  7. iOS 瀑布流的基本原理

    /** * 源代码链接 * 链接: https://pan.baidu.com/s/1nvLamEX 密码: kya5 */ #import <UIKit/UIKit.h> @interf ...

  8. ios瀑布流

    http://blog.csdn.net/shenjx1225/article/details/9037631

  9. iOS开发笔记15:地图坐标转换那些事、block引用循环/weak–strong dance、UICollectionviewLayout及瀑布流、图层混合

    1.地图坐标转换那些事 (1)投影坐标系与地理坐标系 地理坐标系使用三维球面来定义地球上的位置,单位即经纬度.但经纬度无法精确测量距离戒面积,也难以在平面地图戒计算机屏幕上显示数据.通过投影的方式可以 ...

随机推荐

  1. icon踩坑记录

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. cf540D. Bad Luck Island(概率dp)

    题意 岛上有三个物种:剪刀$s$.石头$r$.布$p$ 其中剪刀能干掉布,布能干掉石头,石头能干掉剪刀 每天会从这三个物种中发生一场战争(也就是说其中的一个会被干掉) 问最后仅有$s/r/p$物种生存 ...

  3. SAP增强Enhancement

    第一代:基于源码增强(子过程subroutine) 第一代增强基于源代码,是SAP提供的一个空代码的子过程.在这个子过程中用户可以添加自己的代码,控制自己的需求.这类增强集中在一些文件名倒数第二个字符 ...

  4. MySQL自学笔记_联结(join)

    1.  背景及原因 关系型数据库的一个基本原则是将不同细分数据放在单独的表中存储.这样做的好处是: 1).避免重复数据的出现 2).方便数据更新 3).避免创建重复数据时出错 例子: 有供应商信息和产 ...

  5. ARM协处理器

    协处理器是一种芯片,用于减轻系统微处理器的特定处理任务.例如,数学协处理器可以控制数字处理:图形协处理器可以处理视频绘制.例如,intel pentium微处理器就包括内置的数学协处理器.  协处理器 ...

  6. Linux dmidecode命令

    1.linux系统自带的dmidecode工具查询服务器硬件信息 dmidecode 用于获取服务器的硬件信息,通常是在不打开计算机机箱的情况下使用该命令来查找硬件详细信息 这个命令可以查看内存的几乎 ...

  7. JAVA解析XML有哪几种方法?并简述各自的优缺点

    DOM: 是用与平台和语言无关的方式表示XML文档的官方W3C标准,分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.是基于信息层次的 优点有:由于树在内存中是持久的,因此可以修改它以 ...

  8. uncompressing linux .................................................后没反应解决办法

    编译kernel是的no machine record defined 错误,网上有一些解法,其实都是错误的,以讹传讹.不打算自己写,找到一篇还算靠谱的,转摘一下. 其根本原因是没有在 __proc_ ...

  9. 最短路径(最基础,经典的模板和思想):HDU-2544最短路

    题目: 最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  10. mysql之处理金钱小数点后的多余0

    问题产生原因:我们在做基金项目   产生大量的金钱  在GP首页展示首页信息的时候要求去除多余的0   由于我们在数据库设计的时候查询返回数据 例如18.100000 这种形式  而我们需要将多余的0 ...