美文网首页
不规则CollectionView

不规则CollectionView

作者: 风逝伊不离 | 来源:发表于2017-02-14 21:26 被阅读86次

    首先创建继承于UIView的类

    在.h类中

    #import

    @classIrregularCollectionView;

    @protocolIrregularCollectionViewDelegate

    - (void)didSelectedImage:(NSInteger)index collection:(IrregularCollectionView* )collectionView;

    @end

    @interfaceIrregularCollectionView : UIView

    @property(nonatomic,weak)id delegate;

    @end

    在.m类中

    #import"IrregularCollectionView.h"

    @interfaceIrregularCollectionView ()

    @property(nonatomic,strong) UICollectionView *collectionV;

    @end

    @implementationIrregularCollectionView

    - (instancetype)initWithFrame:(CGRect)frame

    {

    if(self= [superinitWithFrame:frame]) {

    [self.collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    [selfaddSubview:self.collectionV];

    }

    returnself;

    }

    - (UICollectionView *)collectionV {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    flowLayout.minimumLineSpacing =10;

    flowLayout.minimumInteritemSpacing =10;

    if(_collectionV ==nil) {

    _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, MAINWIDTH, kHeightScreenAuto(150)) collectionViewLayout:flowLayout];

    _collectionV.delegate =self;

    _collectionV.dataSource =self;

    _collectionV.scrollEnabled =NO;

    _collectionV.backgroundColor = [UIColor whiteColor];

    }

    return_collectionV;

    }

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return5;

    }

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

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

    if(indexPath.item ==0|| indexPath.item ==1) {

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, MAINWIDTH /2, kHeightScreenAuto(70))];

    imageView.image = [UIImage imageNamed:@"xue1"];

    [cell.contentView addSubview:imageView];

    returncell;

    }else{

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, (MAINWIDTH -12) /3, kHeightScreenAuto(60))];

    imageV.image = [UIImage imageNamed:@"xue2"];

    [cell.contentView addSubview:imageV];

    returncell;

    }

    }

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.item ==0|| indexPath.item ==1){

    returnCGSizeMake((self.frame.size.width -12) /2, kHeightScreenAuto(70));

    }else{

    returnCGSizeMake((self.frame.size.width -24) /3, kHeightScreenAuto(60));

    }

    }

    其次  在跳转有UITableView的页面中,UITableView的初始化要用到UITableViewStylePlain

    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

    如果使用UITableViewStyleGrouped  那么UITableView的Y轴会至少给一个分区的headerView的高度,这算是一个小BUG吧  妄各位熟知下

    相关文章

      网友评论

          本文标题:不规则CollectionView

          本文链接:https://www.haomeiwen.com/subject/dwdfwttx.html