网格

作者: Whatever永不放弃 | 来源:发表于2017-11-23 08:21 被阅读0次

//ViewController.m

#import "ViewController.h"#import "HeaderCollectionReusableView.h"#import "FooterCollectionReusableView.h"#import "MyCell.h"@interface ViewController (){

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//创建流布局对象

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

//设定单元格的大小

Flow.itemSize = CGSizeMake(70, 100);

//设置滚动方向

Flow.scrollDirection = UICollectionViewScrollDirectionVertical;

//设置最小行间隔

Flow.minimumLineSpacing = 100;

//设置最小列间距

Flow.minimumInteritemSpacing = 10;

//设定分区的间距

Flow.sectionInset = UIEdgeInsetsMake(50, 10, 20, 10);

//设置页眉的尺寸

Flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);

//设置页脚的尺寸

Flow.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);

//创建网格对象

UICollectionView *theCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:Flow];

//

theCollect.delegate = self;

theCollect.dataSource = self;

//是否按页滑动

theCollect.pagingEnabled = YES;

theCollect.backgroundColor = [UIColor whiteColor];

[self.view addSubview:theCollect];

//注册单元格

[theCollect registerClass:[MyCell class] forCellWithReuseIdentifier:@"Mycell"];

//注册页眉

[theCollect registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];

//注册页脚

[theCollect registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];

}

#pragma -UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

return 2;

}

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

{

if (section == 0)

{

return 5;

}

else if (section == 1)

{

return 10;

}

return 0;

}

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

{

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

//

cell.Img.image = [UIImage imageNamed:@"2.jpg"];

cell.textLb.text = @"信";

return cell;

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

UICollectionReusableView *reusbleV = nil;

if ([kind isEqualToString:UICollectionElementKindSectionHeader])

{

reusbleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];

HeaderCollectionReusableView *header = (HeaderCollectionReusableView *)reusbleV;

header.titleLb.text = @"我是页眉";

}

else if ([kind isEqualToString:UICollectionElementKindSectionFooter])

{

reusbleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];

FooterCollectionReusableView *Footer = (FooterCollectionReusableView *)reusbleV;

Footer.titleLb.text = @"我是页脚";

}

return reusbleV;

}

@end

//HeaderCollectionReusableView.h

#import@interface HeaderCollectionReusableView : UICollectionReusableView

@property (nonatomic,strong)UILabel *titleLb;

@end

//HeaderCollectionReusableView.m

#import "HeaderCollectionReusableView.h"

@implementation HeaderCollectionReusableView
//单例方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self addSubview:self.titleLb];
    }
    return self;
}
//懒加载
- (UILabel *)titleLb
{
    if (!_titleLb)
    {
        _titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
       
        [_titleLb setTextColor:[UIColor whiteColor]];
    }
    return _titleLb;
}
@end

//FooterCollectionReusableView.m

#import <UIKit/UIKit.h>

@interface FooterCollectionReusableView : UICollectionReusableView
@property (nonatomic,strong)UILabel *titleLb;
@end

//FooterCollectionReusableView.m

#import "FooterCollectionReusableView.h"

@implementation FooterCollectionReusableView
//单例方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self addSubview:self.titleLb];
    }
    return self;
}
//懒加载
- (UILabel *)titleLb
{
    if (!_titleLb)
    {
        _titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
       
        [_titleLb setTextColor:[UIColor whiteColor]];
    }
    return _titleLb;
}
@end

//MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UICollectionViewCell
@property (nonatomic,strong)UIImageView *Img;
@property (nonatomic,strong)UILabel *textLb;
@end

//MyCell.m

#import "MyCell.h"

@implementation MyCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.Img];
        [self addSubview:self.textLb];
    }
    return self;
}
- (UIImageView *)Img
{
    if (!_Img)
    {
        _Img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        _Img.layer.cornerRadius = 80/2;
        _Img.layer.masksToBounds = YES;
    }
    return _Img;
}
- (UILabel *)textLb
{
    if (!_textLb)
    {
        _textLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 20)];
        _textLb.textAlignment = NSTextAlignmentCenter;
        _textLb.textColor = [UIColor blackColor];
       
    }
    return _textLb;
}
@end

相关文章

  • CSS Grid Auto

    关键词:隐式轨道 / 隐式网格 / 自动布局算法 隐式网格 隐式网格是指当网格项目确认在显式网格之外时所创建的网格...

  • GridLayout网格布局

    网格术语 网格线(Grid Lines)网格线组成了网格,他是网格的水平和垂直的分界线。一个网格线存在行或列的两侧...

  • 铜沛

    铜沛网格 铜沛网格

  • 九宫格拖拽

    效果实例 简单了解Grid布局(网格布局) 什么是网格布局 CSS网格布局(又称“网格”),是一种二维网格布局系统...

  • 九宫格拖拽

    效果实例 简单了解Grid布局(网格布局) 什么是网格布局 CSS网格布局(又称“网格”),是一种二维网格布局系统...

  • 网格

    遵守网格的协议 创建网格

  • 11月24日4D分享

    A:加班整理网格基础数据,网格人员表示很无奈,不认可。 M:很郁闷。 B:网格人员被我留下来加班整理网格的基础数据...

  • 10月17日4D8B分享

    A:本日对本网格行销团队在其他网格行销是否回自己网格行销事情纠结不清,无法和网格经理达成一直。 M:心中纠结,难以...

  • 民健社区网格长的一天

    小网格,释放大能量。社区矛盾调解、安全检查,环境整治、小广告清理、网格巡查等诸多事项,无不由网格长协调网格员处理。...

  • 网格化的前世今生

    作者:丁野菠 江安警官学院社会治理专业资深讲师 现在谈到社会治理必谈网格化,还有网格员、网格长、总网格长...

网友评论

      本文标题:网格

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