IOS 流布局

//生成流布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

//生成view视图,类似于Tabview
    UICollectionView *colletionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:colletionview];
//由于前面使用CGRectZero,我使用Masonry进行布局
    [colletionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(LWLWindowsW-10);
        make.height.mas_equalTo(LWLWindowsW+20);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(30);
    }];
    //遵守两个代理协议
    colletionview.delegate = self;
    colletionview.dataSource = self;
    colletionview.backgroundColor = [UIColor whiteColor];
    //注册单元格,只能通过注册的方式,不可以生成的
    [colletionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];

协议的方法,这点和tabview一样

//section个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 9;

}
//每个单元格
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;
}
//选中时候的操作
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值