iOS开发--UICollectionView网格视图
来源:互联网 发布:ubuntu原版和麒麟 编辑:程序博客网 时间:2024/05/23 20:15
UICollectionViewFlowLayout: 确定网格视图的布局
上下左右的间距 : sectionInset(left, top, bottom, right)
每一个Cell的大小 : itemSize(width, height)
横向Cell之间的间距 : minimumInteritemSpacing
纵向Cell之间的间距 : minimumLineSpacing
首先:定制Cell#import "CollectionViewCell.h"@implementation CollectionViewCell- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor lightGrayColor]; self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.frame)-10, CGRectGetHeight(self.frame)-10)]; self.imgView.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self addSubview:self.imgView]; self.text = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(self.imgView.frame)-50, CGRectGetWidth(self.frame)-10, 20)]; self.text.backgroundColor = [UIColor brownColor]; self.text.textAlignment = NSTextAlignmentCenter; [self addSubview:self.text]; self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; self.btn.frame = CGRectMake(5, CGRectGetMaxY(self.text.frame), CGRectGetWidth(self.frame)-10, 30); [self.btn setBackgroundColor:[UIColor orangeColor]]; [self.btn setTitle:@"按钮" forState:UIControlStateNormal]; [self addSubview:self.btn]; } return self;}@end
#define fDeviceWidth [UIScreen mainScreen].bounds.size.width#define fDeviceHeight [UIScreen mainScreen].bounds.size.height#import "JCollectionController.h"#import "CollectionViewCell.h"#import "CustomHeaderView.h"static const float fHeaderHeight = 200.0f;@interface JCollectionController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{ CustomHeaderView *_headerView;}@end@implementation JCollectionController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self setupCollectionView];}- (void)setupCollectionView{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; //头部大小 flowLayout.headerReferenceSize = CGSizeMake(fDeviceWidth, fHeaderHeight); self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, fDeviceWidth , fDeviceHeight) collectionViewLayout:flowLayout]; //注册Cell,必须要有 、、、注册ReuseableView---头部(尾部同理) [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"myCell"]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseableView"]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.view addSubview:self.collectionView]; _headerView = [[CustomHeaderView alloc] initWithFrame:CGRectMake(5, 0, fDeviceWidth-10, fHeaderHeight)]; [self.view addSubview:_headerView];}#pragma mark ---UICollectionViewDataSource/** * UICollectionViewCell的个数 */- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10;}/** * section的个数 */- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2;}/** * 每个UICollectionView展示的内容 */-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString * CellIdentifier = @"myCell"; CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; if (!cell) { NSLog(@"----------------"); } cell.imgView.image = [UIImage imageNamed:@"aaa"]; cell.text.text = [NSString stringWithFormat:@"cell-%ld",indexPath.row]; return cell;}/** * 头部显示的内容 */- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseableView" forIndexPath:indexPath]; [headerView addSubview:_headerView]; return headerView;}#pragma mark --UICollectionViewDelegateFlowLayout/** * 定义每个UICollectionView 的大小 */- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ CGFloat spacing = 10.0; CGFloat itemWidth = (fDeviceWidth - spacing *3)/2.0; CGFloat itemHeight = itemWidth; return CGSizeMake(itemWidth,itemHeight);}/** * 定义每个UICollectionView 的 margin */-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);}/** * 定义每个UICollectionView 的横向间距 */- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 10;}/** * 定义每个UICollectionView 的 竖向间距 */- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 10;}#pragma mark --UICollectionViewDelegate/** * UICollectionView被选中时调用的方法 */-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; NSLog(@"点击:{区%ld,%ld个}宽:%f",indexPath.section,indexPath.row,cell.frame.origin.x);}/** * 返回这个UICollectionView是否可以被选择 */-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}@end
0 0
- iOS开发--UICollectionView网格视图
- UICollectionView(网格视图)
- iOS开发 -- UICollectionView(集合视图)
- iOS开发:集合视图 UICollectionView
- iOS开发-------自定义简单的表情键盘(UICollectionView 集合视图)
- iOS开发之高级视图—— UICollectionView
- iOS编程------集合视图UICollectionView
- OCiOS开发:集合视图 UICollectionView
- iOS开发- UICollectionView详解
- UICollectionView使用 - iOS开发
- iOS开发- UICollectionView详解
- IOS开发之UICollectionView
- IOS开发之UICollectionView
- iOS开发-UICollectionView
- iOS开发 ----- UICollectionView
- iOS开发之UICollectionView
- iOS开发之UICollectionView
- iOS开发基础 - UICollectionView
- 在 SystemUI 的 Statusbar 中 添加 ethernet status icon
- js中常见数据类型输出情况
- 水桶定律
- 工作周报051
- 计算机视觉领域稍微容易中的期刊(二)
- iOS开发--UICollectionView网格视图
- mysql怎么查询条件以另一个表中条件进行查询
- Floyd算法:用动态规划求解多源(全源)最短路径
- 4.2 内核工作队列原理汇总
- Android下的挂钩(hook)和代码注入(inject)
- 蘑菇管理
- 源码安装erlang
- java中的强引用、软引用、弱引用、虚引用
- Ubuntu Linux下设置IP的配置命令