UICollectView

1 :初始化 视图布局

    //1.创建UICollectionViewFlowLayout,
    //对cell进行布局控制
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    //设置
    //1.1设置大小
    flowLayout.itemSize=CGSizeMake(90, 90);
    //1.2设置列间距(注意如果给定距离的间距,无法满足屏幕宽度,设置无效)
    flowLayout.minimumInteritemSpacing=0;
    //1.3设置行间距
    flowLayout.minimumLineSpacing=10;
    //1.4设置滑动方向
   //flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    //1.5 Header
    flowLayout.headerReferenceSize=CGSizeMake(self.bounds.size.width, 100);
    //1.6 Footer
    flowLayout.footerReferenceSize=CGSizeMake(self.bounds.size.width, 100);
    
    //1.7设置内边距
    flowLayout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    
    
    
    
    //2.创建集合视图
    self.coll=[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
    //背景颜色默认为黑色
    self.coll.backgroundColor=[UIColor purpleColor];
    
    //添加到视图上
    [self addSubview:self.coll];

自定义Cell

#import "myCell.h"

@implementation myCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addAllViews];
        
        
        
    }
    return self;
}
//试图布局
-(void)addAllViews{

  

    //视图空间都要加载到contentView上
    self.imv=[[UIImageView alloc] init];
    self.imv.frame=CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.8);
    self.imv.image=[UIImage imageNamed:@"89.png"];
    self.imv.backgroundColor=[UIColor orangeColor];
    [self.contentView addSubview:self.imv];
    
    self.label=[[UILabel alloc] init];
    self.label.frame=CGRectMake(0, CGRectGetMaxY(self.imv.frame), self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.2);
    self.label.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:self.label];
    


}
//一旦重新显示就执行
-(void)layoutSubviews{

    [super layoutSubviews];
    self.imv.frame=CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.8);
    self.label.frame=CGRectMake(0, CGRectGetMaxY(self.imv.frame), self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.2);


}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


控制器里边

#import "RootViewController.h"
#import "RootView.h"
#import "myCell.h"
#define kCollCell @"collCell"
#define kHeaderView @"headerView"
#define kFooterView @"footerView"
//遵循集合视图代理.和数据源协议
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//FlowLayout不需要设置代理



@property(nonatomic,strong)RootView *rv;
@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置代理,
    self.rv.coll.dataSource=self;
    self.rv.coll.delegate=self;
    
    //注册cell
    [self.rv.coll registerClass:[myCell class] forCellWithReuseIdentifier:kCollCell];
    
    //注册header
    [self.rv.coll registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView];
    //注册footer
    [self.rv.coll registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView];

       
    
    
}

//每隔分组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 103;

}

//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    //创建cell改为自己的cell
    myCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:kCollCell forIndexPath:indexPath];
   // float red=arc4random()%256/255.0;
    
//    cell.imv.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//    cell.label.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    
    
    return cell;


}

//设置选中的Cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section:%d row:%d",indexPath.section,indexPath.row);

}

//设置header和footer
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind==UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView forIndexPath:indexPath];
       
        UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
        lab.backgroundColor=[UIColor blackColor];
        [header addSubview:lab];
        
        header.backgroundColor=[UIColor orangeColor];
        return header;
    }
    else{
    
        UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView forIndexPath:indexPath];
        
        footer.backgroundColor=[UIColor yellowColor];
        return footer;
        
    
    }

}










//设置frame
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row==1) {
        return CGSizeMake(90, 90);
    }else{
    
        return CGSizeMake(50, 50);
    }
}

//设置四周边距
-(UIEdgeInsets )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(1, 1, 1, 1);

}

//行边距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;

}

//列边距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 50;
}

//footer的size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(320, 50);

}
//header的size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(320, 40);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值