Skip to content

Instantly share code, notes, and snippets.

@gliubc
Last active December 16, 2018 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gliubc/d815b8bfcc6c7dc7a92c2c9102ccee09 to your computer and use it in GitHub Desktop.
Save gliubc/d815b8bfcc6c7dc7a92c2c9102ccee09 to your computer and use it in GitHub Desktop.
#import "LNCustomCollectionView.h"
// 单元格
@interface HomeMenuCell : UICollectionViewCell
// 图片
@property (strong, nonatomic) UIImageView *imageView;
// 文本
@property (strong, nonatomic) UILabel *label;
@end
// 视图
@interface HomeMenuCollectionView : LNCustomCollectionView
@end
#import "HomeMenuCollectionView.h"
// 单元格
@implementation HomeMenuCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 图片
UIImageView *imageView = [UIImageView new];
imageView.layer.masksToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.contentView addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.equalTo(@59);
make.top.offset(15);
make.centerX.offset(0);
}];
self.imageView = imageView;
// 文本
UILabel *label = [UILabel new];
label.font = [UIFont boldSystemFontOfSize:14];
label.textColor = HEXCOLOR(0x333333);
label.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imageView.mas_bottom).offset(0);
make.left.right.offset(0);
}];
self.label = label;
}
return self;
}
@end
// 视图
@implementation HomeMenuCollectionView
- (void)commonInit {
[super commonInit];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
[self registerClass:[HomeMenuCell class] forCellWithReuseIdentifier:@"HomeMenuCell"];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeMenuCell" forIndexPath:indexPath];
id item = self.itemArray[indexPath.row];
if (self.bind) {
self.bind(item, cell);
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger kItemNumOfRow = 4;
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
NSInteger row = indexPath.row;
CGFloat totalWidth = self.frame.size.width - layout.sectionInset.left - layout.sectionInset.right - layout.minimumInteritemSpacing * (kItemNumOfRow - 1);
CGFloat width;
if (row % kItemNumOfRow) {
width = roundf(totalWidth / kItemNumOfRow);
} else {
width = totalWidth - roundf(totalWidth / kItemNumOfRow) * (kItemNumOfRow - 1);
}
return CGSizeMake(width, 100);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment