不规则CollectionView

2017-02-14  本文已影响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吧  妄各位熟知下

上一篇 下一篇

猜你喜欢

热点阅读