瀑布流第三方使用流程首先将第三方库拉入工程
woutflow.h
waterFlowLayoutDelegate
SDWebImage
ViewController.m
#import "ViewController.h"
#import "woutflow.h"
#import "UIImageView+WebCache.h"
#import "TwoCollectionViewCell.h"
#import "Model.h"@interface ViewController ()
{ NSMutableDictionary *dataDic; NSMutableArray *theArr; NSString *theWidth; NSString *theHeight; UICollectionView *theCollectionView; NSMutableArray *theArrTwo;}
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{//刷新控件 [theCollectionView reloadData];}
- (void)viewDidLoad
{ [super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//初始化文件 woutflow *theW = [[woutflow alloc]init];
theW.delegate = self;
//初始化网格 theCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:theW]; theCollectionView.dataSource = self;
[theCollectionView registerClass:[TwoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:theCollectionView];
//获取plist文件内容 NSString *path = [[NSBundle mainBundle] pathForResource:@"photos" ofType:@"plist"];
dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theArr = [dataDic objectForKey:@"content"];
theArrTwo = [[NSMutableArray alloc]init];
//遍历Model for (NSDictionary * dic in theArr)
{ Model * model = [[Model alloc]init];
[model setValuesForKeysWithDictionary:dic]; [theArrTwo addObject:model]; } }
#pragma mark --- collectionView datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return theArrTwo.count;}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ TwoCollectionViewCell *theC = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
NSInteger tag = 10;
theC.theImage = [theC.contentView viewWithTag:tag];
[theC.theImage setTag:tag];
[theC.contentView addSubview:theC.theImage];
theC.backgroundColor = [UIColor redColor];
theC.theImage.backgroundColor = [UIColor greenColor];
//model赋值
Model * mm=theArrTwo[indexPath.row];
theC.theModel = mm;
return theC;}
#pragma mark --- water Flow Lay out Delegate
-(CGFloat)waterFlowlayOut:(woutflow *)layout heightForItemAtIndex:(NSIndexPath * )indexPath itemWidth:(CGFloat)itemWidth
{//根据图片大小获取高度 return [[theArr[indexPath.item] objectForKey:@"height"] floatValue] / [[theArr[indexPath.row] objectForKey:@"width"] floatValue] * itemWidth; }
@end
创建model,并且将对应赋值
Model.h
#import
#import
@interface Model : NSObject
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *url;
@property(nonatomic,assign)CGFloat width;
@property(nonatomic,assign)CGFloat height;
//创建CollectionViewCell.h.
#import "Model.h"
@interface TwoCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *theImage;
@property(nonatomic,strong)Model *theModel;
@end
//创建CollectionViewCell.m。
#import "TwoCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@implementation TwoCollectionViewCell
//重写单元格
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame])
{
[self.contentView addSubview:self.theImage];
}
return self;
}
//model赋值
-(void)setTheModel:(Model *)theModel
{
_theModel=theModel;
_theImage.frame = CGRectMake(3, 3, self.contentView.frame.size.width-6, self.frame.size.height-6);
[_theImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_theModel.url]]];
}
//初始化图片
-(UIImageView *)theImage
{
if (!_theImage) {
self.theImage = [[UIImageView alloc]init];
}
return _theImage;
}@end