瀑布流

#import "ViewController.h"

//网格第三方

#import

//sdwebimage

#import

//单元格样式类

#import "CollectionViewCell.h"

//模型类#import "Model.h"

//签署协议

@interface ViewController(){

NSInteger i;

UICollectionView *collec;

}

@property(nonatomic,strong)NSMutableArray *arr;

@end

static NSString *cellID = @"celll";

@implementation ViewController

//懒加载数组

-(NSMutableArray *)arr

{

if (!_arr) {

//初始化数组

_arr = [[NSMutableArray alloc] init];

//读取plist文件当中的数据

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"plist"];

//数据存储到arr1当中

NSArray *arr1 = [[NSArray alloc] initWithContentsOfFile:path];

//遍历数组字典转模型

for (NSDictionary *dic in arr1) {

Model *mm = [Model imagedata:dic];

//将model添加到数组

[_arr addObject:mm];

}

}

return _arr;

}

- (void)viewDidLoad {

[super viewDidLoad];

i = 1;

//初始化导航栏按钮

UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"ADD" style:UIBarButtonItemStylePlain target:self action:@selector(add)];

self.navigationItem.rightBarButtonItem = right;

//声明瀑布流有几列

XRWaterfallLayout *layout = [XRWaterfallLayout waterFallLayoutWithColumnCount:3];

//单元格之间的间距

[layout setColumnSpacing:5 rowSpacing:5 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];

//签署代理

layout.delegate =self;

collec = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];

collec.delegate =self;

collec.dataSource = self;

collec.backgroundColor = [UIColor whiteColor];

[self.view addSubview:collec];

[collec registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellID];

}

//添加按钮点击事件

-(void)add

{

i++;

if (i>_arr.count) {

UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经到底了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

[aler show];

}

else

{

[collec reloadData];

}

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return i;

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

cell.mm = self.arr[indexPath.item];

return cell;

}

- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath

{

Model *mm = self.arr[indexPath.item];

return mm.h/mm.w*itemWidth;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(瀑布流)