iphone设计越来越多的用到了瀑布流,实现瀑布流的方法有多中,现在使用的多是UITableview和UIScrollView两种方法,可是,从ios 6.0之后出现了一种牛逼到不行的方法:UICollectionView
下面从n个方面来进行UICollectionView的学习:
step1 :使用 流程
(1):设置一个layout(例如:UICollectionViewFlowLayo
ut)
(2):指定datasource并实现requried方法
(3):因为要重用,所以需要registClass并给出重用标识
(4):cell一般要自定义
step2:代码实现
(1):我们创建一个工程,在工程中创建一个视图控制器 RootViewController
(2):重写父类的loadView方法(这个方法的唯一作用就是为self.view赋值)
注:将重用标识设置成静态全局变量:
static NSString *key = @"identify";//重用标识
static NSString *key1 = @"header";
static NSString *key2 = @"footer";
-(void)loadView
{
// 1 设置一个layout
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
// 2 创建以个集合视图类(UICollectionView)
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flow];
//注册:告诉系统哪个类创建重用标识,标识是什么,创建什么类型的cell
[collectionView registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:key];
//下面代码是设置分区Header 和 Footer的第一步:注册
[collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:key1];
[collectionView registerClass:[foolter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:key2];
self.view = collectionView; //意义:说明自带一个瀑布流
self.view.backgroundColor = [UIColor clearColor]; //默认的是黑色
//dataSource和delegate需要在.h文件中遵循相应地协议(@interface RootViewController :UIViewController<</span>UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>)并实现协议里面的方法
collectionView.dataSource = self;
collectionView.delegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - datesource
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
// 这里面我们使用了自定义的cell类,所以,我们要创建这样的一个类
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:keyforIndexPath:indexPath];//不会为空,若为空自动创建了
// contentView property
// backgroundView property
// selectedBackgroundView property
// Managing the Cell’s State
// selected property
// highlighted property
cell.contentView.backgroundColor = [UIColor yellowColor];
cell.label.text = [NSString stringWithFormat:@"%d %d", indexPath.section,indexPath.row];
return cell;
}
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 10;
}
//step2: 设置分区header 和 footer 的第二步 :重用 此处我们用到了Header类和Footer类是需要我们自行创建的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
HeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:key1 forIndexPath:indexPath];//需要zhuce
header.label.text = @"AAA";
return header;
}else{
foolter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:key2 forIndexPath:indexPath];
footer.label.text = @"BBB";//step2:
return footer;
}
}
#pragma mark - delegate
// step3 : 设置分区header 和 footer 第三步:指定大小://设置header的高度(注册,重用,指定大小)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(320, 60);//宽默认
}
//step3 指定大小://设置footer的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(100, 60);//宽默认
}
//上下间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
// return 10;
//}
//行间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
// return 100;
//}
//每个视图的宽高
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
// return CGSizeMake(100, 100);
//}
设置整个分区相对上下左右的间距
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
// return UIEdgeInsetsMake(50, 50, 50, 50);
//}
@end
运行效果如下图: