关于网格的开发环境
代码如下:
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "FooterCollectionReusableView.h"
#import <Masonry.h>
@interface ViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建流布局对象
UICollectionViewFlowLayout *theFlow = [[UICollectionViewFlowLayout alloc]init];
// 设置单元格的大小
theFlow.itemSize = CGSizeMake(80, 100);
// 设定滚动方向
theFlow.scrollDirection = UICollectionViewScrollDirectionVertical;
// 最小列间距
theFlow.minimumInteritemSpacing = 10;
// 最小列间距
theFlow.minimumLineSpacing = 10;
// 设置分区边距
theFlow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// 设置页眉的尺寸
theFlow.headerReferenceSize = CGSizeMake(40, 30);
// 设置页脚的尺寸
theFlow.footerReferenceSize = CGSizeMake(40, 30);
// 创建网格对象
UICollectionView *theColletion = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:theFlow];
// 设置代理
theColletion.delegate = self;
theColletion.dataSource = self;
// 设置可以滚动
theColletion.pagingEnabled = YES;
// 添加到视图上
[self.view addSubview:theColletion];
[theColletion mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// 为网格注册单元格类
[theColletion registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
// 为网格注册页眉类
[theColletion registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
// 为网格注册页脚类
[theColletion registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
#pragma -
#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *theCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
// 设置显示数据
theCell.theImg.image = [UIImage imageNamed:@"3.jpg"];
theCell.theLabel.text = @"666";
return theCell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *theReusable = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
theReusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
HeaderCollectionReusableView *theHeader = (HeaderCollectionReusableView *)theReusable;
theHeader.theTitleLabel.text = @"页眉";
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
theReusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
FooterCollectionReusableView *theFooter = (FooterCollectionReusableView *)theReusable;
theFooter.theTitleLabel.text = @"页脚";
}
return theReusable;
}
@end
代码如下:
#import <UIKit/UIKit.h>
@interface CollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *theImg;
@property(nonatomic,strong)UILabel *theLabel;
@end
代码如下:
#import "CollectionViewCell.h"
@implementation CollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.theImg];
[self addSubview:self.theLabel];
}
return self;
}
- (UIImageView *)theImg
{
if (!_theImg)
{
_theImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
}
return _theImg;
}
- (UILabel *)theLabel
{
if (!_theLabel)
{
_theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 20)];
_theLabel.font = [UIFont systemFontOfSize:18];
_theLabel.backgroundColor = [UIColor blueColor];
_theLabel.textAlignment = NSTextAlignmentCenter;
}
return _theLabel;
}
@end
代码如下:
#import <UIKit/UIKit.h>
@interface HeaderCollectionReusableView : UICollectionReusableView
@property(nonatomic,strong)UILabel *theTitleLabel;
@end
#import "HeaderCollectionReusableView.h"
@implementation HeaderCollectionReusableView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.theTitleLabel];
}
return self;
}
-(UILabel *)theTitleLabel{
if (!_theTitleLabel) {
_theTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
_theTitleLabel.textColor = [UIColor whiteColor];
}
return _theTitleLabel;
}
@end
代码如下:
#import <UIKit/UIKit.h>
@interface FooterCollectionReusableView : UICollectionReusableView
@property(nonatomic,strong)UILabel * theTitleLabel;
@end
代码如下
#import "FooterCollectionReusableView.h"
@implementation FooterCollectionReusableView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.theTitleLabel];
}
return self;
}
-(UILabel *)theTitleLabel{
if (!_theTitleLabel) {
_theTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
_theTitleLabel.textColor = [UIColor whiteColor];
}
return _theTitleLabel;
}
@end