博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UICollectionView
阅读量:6820 次
发布时间:2019-06-26

本文共 5377 字,大约阅读时间需要 17 分钟。

#import "ViewController.h"

#define ID @"reuse"
#import "MyCollectionViewCell.h"
#import "MyCollectionReusableView.h"
#define kWidth self.view.frame.size.width
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kWidth - 40) / 3, 100);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 20;
}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 20;
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个布局对象,采用系统布局类UICollectionViewFlowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
//    //设置最小的行间距
//    layout.minimumLineSpacing = 10;
//    
//    //设置Item之间间距
//    layout.minimumInteritemSpacing = 10;
//    //设置集合视图的分区间隔
//    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //设置集合视图的滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    CGFloat totalWidth = self.view.frame.size.width;
    
    
    //设置每一个Item的尺寸大小
//    layout.itemSize = CGSizeMake((totalWidth - 40) / 3, 80);
    layout.headerReferenceSize = CGSizeMake(totalWidth, 40);
    
    //集合视图的创建必须指定布局,如果没有布局,显示不了任何东西
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    
    //集合视图如果想要显示内容,必须将cell注册
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:ID];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    //如果想要分区头视图显示,必须注册增广视图
    [collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    [self.view addSubview:collectionView];
    collectionView.backgroundColor = [UIColor grayColor];
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 60;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *ID = @"reuse";
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
        cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random()  % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
    cell.numberLabel.text = [NSString stringWithFormat:@"道无涯第%ld步",indexPath.row + 1];
    return cell;
}
//设置分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
//返回增广视图,也就是集合视图的头视图或者尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    view.backgroundColor  = [UIColor blueColor];
    view.headerLabel.text = [NSString stringWithFormat:@"仙逆第%ld部 ",indexPath.section + 1];
    return view;
    
    
}
#pragma mark -----集合视图代理方法----
//Item点击之后触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld %ld",indexPath.section,indexPath.row);
}
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //获取整体Item的宽和高
        CGFloat totalWidth = frame.size.width;
        CGFloat totalHeight = frame.size.height;
        self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, totalWidth, totalHeight - 20)];
        self.numberLabel.textAlignment = NSTextAlignmentCenter;
        self.numberLabel.font = [UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:self.numberLabel];
    }
    return self;
}
@end
#import "MyCollectionReusableView.h"
@implementation MyCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.headerLabel = [[UILabel alloc] initWithFrame:self.bounds];
        self.headerLabel.textAlignment = NSTextAlignmentCenter;
        self.headerLabel.font = [UIFont boldSystemFontOfSize:36];
        [self addSubview:self.headerLabel];
    }
    return self;
}
@end

转载于:https://www.cnblogs.com/networkprivaate/p/5204417.html

你可能感兴趣的文章
swiper的基础使用(五)
查看>>
Windows Server 2012R2 Hyper-v之虚拟机复制(2)
查看>>
大数据各种实用网站
查看>>
Linux系统启动过程
查看>>
使用Dnsmasq 部署GPXE 安装 Centos7
查看>>
我的友情链接
查看>>
Windows 2012 Hyper-V Step by Step (四) 创建iSCSI映射
查看>>
我的友情链接
查看>>
Nginx+Keepalived(带Nginx监控脚本)
查看>>
我的友情链接
查看>>
利用SVN的post-commit钩子实现多项目自动同步
查看>>
linux 的ping 命令
查看>>
java基础
查看>>
反射之获取类,方法等
查看>>
TechEd 2012 微软技术大会简介
查看>>
ajax框架之DWR项目运行报错之org.apache.commons.logging.LogFactory
查看>>
终端市场消费减少
查看>>
鲜果CEO梁公军:Google Reader的用户是我们很看重的机会
查看>>
cocos2d-x3.0beta版+NDK-r9b在android上的启动过程
查看>>
基于Spring MVC+Spring JPA技术实战开发大型商业ERP项目教程
查看>>