UI第二十一天:集合视图

// 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;
    }
   
}
//实现等比缩放根据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;
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值