// CocoPods
添加第三方框架的
-(void)addUICollectionView
{
//创建一个网状结构布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];
// 设置网状结构的具体属性
// 设置行间距
layout.minimumLineSpacing =20;
// 设置最小列间距
layout.minimumInteritemSpacing =20;
//item 的大小
layout.itemSize =CGSizeMake(140,200);
//表头的size
layout.headerReferenceSize =CGSizeMake(100,200);//x是横屏的y是竖屏的
//表尾的size
layout.footerReferenceSize =CGSizeMake(100,300);
//滚动方向(默认是竖着的)
layout.scrollDirection =UICollectionViewScrollDirectionVertical;
//内边距
layout.sectionInset =UIEdgeInsetsMake(30,30,30,30);
//设置item强制size
// layout.estimatedItemSize = CGSizeMake(10, 20);
//创建collectionView
UICollectionView *collectionView = [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:layout];
// 设置代理
collectionView.dataSource =self;
collectionView.delegate =self;
//设置背景颜色不给是黑的
collectionView.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:collectionView];
[collectionView release];
[layout release];
// 注册要使用的cell
[collectionView registerClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:@"MyCell"];
// 注册表头
[collectionView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"MyHead"];
// 注册表尾
[collectionView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"MyFoot"];
}
#pragma mark--设置代理
// 设置每个分区多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return10;
}
//返回每个索引下UICollectionViewCell的样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 重用池有可以用的就返回给你如果没有就创建一个
//必须注册一下要使用的标识符的cell才能使用
//系统才知道要从重用池里取出那个类型的cell
UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"MyCell"forIndexPath:indexPath];
cell.backgroundColor = [UIColorredColor];
return cell;
}
//设置表头和表尾
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//判断是表头还是表尾类型
if([kindisEqualToString:UICollectionElementKindSectionHeader])
{
//相当于去重用池中判断一下有就用没有就建
//UICollectionElementKindSectionHeader
//表头类型
//可重用的View分为2种一种表头一种表尾
UICollectionReusableView *headView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"MyHead"forIndexPath:indexPath];
headView.backgroundColor = [UIColorblueColor];
return headView;
}else
{
//表尾
UICollectionReusableView *footView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"MyFoot"forIndexPath:indexPath];
footView.backgroundColor = [UIColororangeColor];
return footView;
}
}
{
//创建一个网状结构布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];
// 设置网状结构的具体属性
// 设置行间距
layout.minimumLineSpacing =20;
// 设置最小列间距
layout.minimumInteritemSpacing =20;
//item 的大小
layout.itemSize =CGSizeMake(140,200);
//表头的size
layout.headerReferenceSize =CGSizeMake(100,200);//x是横屏的y是竖屏的
//表尾的size
layout.footerReferenceSize =CGSizeMake(100,300);
//滚动方向(默认是竖着的)
layout.scrollDirection =UICollectionViewScrollDirectionVertical;
//内边距
layout.sectionInset =UIEdgeInsetsMake(30,30,30,30);
//设置item强制size
// layout.estimatedItemSize = CGSizeMake(10, 20);
//创建collectionView
UICollectionView *collectionView = [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:layout];
// 设置代理
collectionView.dataSource =self;
collectionView.delegate =self;
//设置背景颜色不给是黑的
collectionView.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:collectionView];
[collectionView release];
[layout release];
// 注册要使用的cell
[collectionView registerClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:@"MyCell"];
// 注册表头
[collectionView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"MyHead"];
// 注册表尾
[collectionView registerClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"MyFoot"];
}
#pragma mark--设置代理
// 设置每个分区多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return10;
}
//返回每个索引下UICollectionViewCell的样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 重用池有可以用的就返回给你如果没有就创建一个
//必须注册一下要使用的标识符的cell才能使用
//系统才知道要从重用池里取出那个类型的cell
UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"MyCell"forIndexPath:indexPath];
cell.backgroundColor = [UIColorredColor];
return cell;
}
//设置表头和表尾
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//判断是表头还是表尾类型
if([kindisEqualToString:UICollectionElementKindSectionHeader])
{
//相当于去重用池中判断一下有就用没有就建
//UICollectionElementKindSectionHeader
//表头类型
//可重用的View分为2种一种表头一种表尾
UICollectionReusableView *headView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"MyHead"forIndexPath:indexPath];
headView.backgroundColor = [UIColorblueColor];
return headView;
}else
{
//表尾
UICollectionReusableView *footView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"MyFoot"forIndexPath:indexPath];
footView.backgroundColor = [UIColororangeColor];
return footView;
}
}
//实现等比缩放根据model修改当前的item的高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
GirlModel *model =self.arr[indexPath.row];
CGFloat width = model.width;
CGFloat height = model.height;
CGFloat scale =130 / width;
CGSize size =CGSizeMake(150 ,height * scale );
return size;
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
GirlModel *model =self.arr[indexPath.row];
CGFloat width = model.width;
CGFloat height = model.height;
CGFloat scale =130 / width;
CGSize size =CGSizeMake(150 ,height * scale );
return size;
}