瀑布流, 和tableview类似,但可以一行铺多个
MyCell.h
#import <UIKit/UIKit.h>
@interface MyCell : UICollectionViewCell
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel *label;
@end
MyCell.m
#import "MyCell.h"
@implementation MyCell
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self createView];
}
return self;
}
-(void)createView{
self.label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/3)];
[self addSubview:self.label];
self.picImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/3, self.frame.size.width, self.frame.size.height/3*2)];
[self addSubview:self.picImageView];
}
MyReusableView.h
#import <UIKit/UIKit.h>
@interface MyReusableView : UICollectionReusableView
@property(nonatomic,retain)UIView *myView;
@end
MyReusableView.m
#import "MyReusableView.h"
@implementation MyReusableView
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
self.myView=[[UIView alloc] init];
self.myView.backgroundColor=[UIColor yellowColor];
[self addSubview:self.myView];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.myView.frame=CGRectMake(0, 0, 100, 100);
}
@end
ViewController.m
#import "ViewController.h"
#import "MyCell.h"
#import "UIImageView+WebCache.h"
#import "MyReusableView.h"
@interface ViewController ()<UICollectionViewDataSource,UIScrollViewDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//官方提供的一种瀑布流效果
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
//这里对每一个用来显示的区域成为item,就是tableview上的cell
//设置一下item的尺寸
flowLayout.itemSize=CGSizeMake(100, 120);
//通过flowLayout来设置样式
//设置最小行间距
flowLayout.minimumLineSpacing=2;
flowLayout.minimumInteritemSpacing=2;
//边框
flowLayout.sectionInset=UIEdgeInsetsMake(20, 20, 20, 20);
//设置flowlayout头部滚动范围
flowLayout.headerReferenceSize=CGSizeMake(0, 300);
//设置一下滑动方向
//UICollectionViewScrollDirectionHorizontal水平滑动
// flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
UICollectionView *collestionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.view addSubview:collestionView];
//设置代理人
collestionView.delegate=self;
collestionView.dataSource=self;
[collestionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuse"];
//注册头部或尾部的区域
//参数一:需要注册的类型是UICollectionReusableView
//参数二:指定注册视图的类型,是头还是尾视图
//参数三:重用标志
[collestionView registerClass:[MyReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
[self createData];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
MyReusableView *view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
view.backgroundColor=[UIColor orangeColor];
return view;
}
-(void)createData{
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
NSData *data=[NSData dataWithContentsOfFile:path];
self.arr=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",self.arr);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
cell.backgroundColor=[UIColor cyanColor];
cell.label.text=[NSString stringWithFormat:@"%ld",indexPath.row];
[cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"thumbURL"]]];
return cell;
}