CollectionView 详细用法
来源:互联网 发布:java回调机制 编辑:程序博客网 时间:2024/06/07 05:42
#import "ViewController.h"#import "CollectionReusableView.h"#define SCREEN_WIDTH [[UIScreen mainScreen]bounds].size.width#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.// flowLayout 控制 布局 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];// section 内置大小 flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);// 最小列间距 默认最小是10, 如果小于10才需要写, 想要大于10的话就不用写这个了 ,它会自己去算的 ,行间距类同 flowLayout.minimumInteritemSpacing = 5;// 最小行间距 flowLayout.minimumLineSpacing = 5;// item 大小, 如果只有一种size,不用代理方法返回大小// flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 3.0, 200);// 多种item大小 使用预铺可以提高代码运算效率,提高流畅度// 预铺大小 (如果有一个 50,有一个60, 那么通常设置预估 55, 这样可以节省时间) flowLayout.estimatedItemSize = CGSizeMake(((SCREEN_WIDTH - 30) / 3.0 + SCREEN_WIDTH) / 2.0, 200);// 头部大小 flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 100); flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 80); UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout]; collectionV.dataSource = self; collectionV.delegate = self;// 允许多选 collectionV.allowsMultipleSelection = YES; collectionV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:collectionV]; [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];// header Footer 也叫 增补视图// collectionView 叫集合视图// tableView 叫表视图// collectionView 的header Footer 同cell 一样 需要注册 [collectionV registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"]; // collectionView 的header Footer 同cell 一样 需要注册 [collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];}//区数- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 5;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 5;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];// isSelected 可以记录是否被点击 if (cell.isSelected) { cell.backgroundColor = [UIColor greenColor]; } else { cell.backgroundColor = [UIColor orangeColor]; } return cell;}// 增补视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {// head 视图 if (kind == UICollectionElementKindSectionHeader) { CollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath]; headView.backgroundColor = [UIColor grayColor]; return headView; }// foot视图 if (kind == UICollectionElementKindSectionFooter) { UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath]; footView.backgroundColor = [UIColor yellowColor]; return footView; } return nil;}//每个item 的大小 可以在这个方法里单独设置- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { // item 大小 return CGSizeMake((SCREEN_WIDTH - 30) / 3.0, 200); } else { return CGSizeMake(SCREEN_WIDTH - 20, 200); }}//点击触发- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {// 获取某个cell UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor greenColor];}// 取消点击- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { // 获取某个cell UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor orangeColor];}
0 0
- CollectionView 详细用法
- collectionView的用法
- CollectionView
- collectionView
- collectionView
- collectionView
- CollectionView
- CollectionView
- collectionView
- collectionView
- CollectionView
- collectionView
- collectionview
- Customised collectionView
- collectionView学习
- iOS CollectionView
- iOS---collectionView
- collectionView 小结
- 线段树小结
- html图片热点
- 栈的压入、弹出序列
- MapServer 之 绘制地图图例
- java中的异常处理
- CollectionView 详细用法
- 从上往下打印二叉树
- PHP.INI常用设置一览表(持续更新)
- iOS系列UI篇——UIDatePicker和UIPickerView
- 没有选择Debug构建方式,为文件的某行设置断点可能会失败
- c++实训报告之高校工资管理系统
- 二叉搜索树的后序遍历序列
- oracle 设置自增字段
- UIButtonn-SB