collectionView(手写)
来源:互联网 发布:常用端口号8000 编辑:程序博客网 时间:2024/04/29 23:24
// RootViewController.m
// UI-day12-UICollectionView初认识
//
// Created by liufy on 15/8/4.
// Copyright (c) 2015年 liufy. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UICollectionViewLayout是一个抽象类,里面定义了很多方法和属性,但是不一定有实现,自定义布局需要继承这个类
//使用系统写好的一个瀑布流布局
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
/*
UICollectionViewScrollDirectionVertical,
UICollectionViewScrollDirectionHorizontal
*/
//设置滑动的方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置item之间的最小间距(竖直滑动的时候,表示的横向间距,水平滑动的时候,表示的是纵向间距)
layout.minimumInteritemSpacing = 35;//长的相反
//设置item之间的最小间距(竖直滑动的时候,表示的纵向间距,水平滑动的时候,表示的是横向间距)
layout.minimumLineSpacing = 10;//短的相同
//实例化一个UICollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//设置代理
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:collectionView];
//注册cell(手写版本)
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//注册collectionView的头部视图
//UICollectionReusableView 专门用于充当collectionView的头部和尾部视图
//UICollectionElementKindSectionHeader:标识注册的是头部视图
//UICollectionElementKindSectionFooter:标识注册的是尾部视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
#pragma mark- UIColloctionViewDelegate
//返回组数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
//返回每组当中所有的元素个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 12;
}
//返回每个元素所使用的UICollectionViewCell对象
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//去复用队列中查找cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//改变背景色
cell.backgroundColor = [UIColor orangeColor];
return cell;
}
//修改每一个item的宽度和高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100);
}
//返回每一组元素跟屏幕4个边界的距离(上,左,下,右)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//创建一个UIEdgeInsets的结构体
return UIEdgeInsetsMake(10, 10, 10, 10);
}
//返回头部视图的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//注意:如果是竖直滑动,则只有高度有效,如果是水平滑动,则只有宽度有效
return CGSizeMake(50, 50);
}
//返回头部和尾部视图所使用的UICollectionReusableView对象
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//由于头部和尾部视图的实例化都需要调用此方法,所以需要通过kind判断生成的是头部视图还是尾部视图
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//表示需要创建头部视图
//复用形式创建头部视图
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//改变背景色
headerView.backgroundColor = [UIColor greenColor];
return headerView;
}
return nil;
}
//选中某一个item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"第%ld组第%ld个",indexPath.section,indexPath.item);
}
//效果同layout.minimumLineSpacing,二选一
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//
//}
//效果同layout.minimumInteritemSpacing,二选一
//-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//
//
//}
@end
- collectionView(手写)
- collectionView 手写初始化
- 手写CollectionView并调整布局
- CollectionView
- collectionView
- collectionView
- collectionView
- CollectionView
- CollectionView
- collectionView
- collectionView
- CollectionView
- collectionView
- collectionview
- 手写
- Customised collectionView
- collectionView学习
- iOS CollectionView
- 赢在职场-WEB全栈开发-1-9 文本样式概览
- PAT 1009
- android学习之路---java部分上转型的多态
- CentOS 6.x RHCS Cluster 配置
- Apriori算法的python实现
- collectionView(手写)
- JAVA-WEB开发第五讲[2016-06-04]SPRING-MVC环境搭建
- LeetCode 76. Minimum Window Substring
- html列表相关属性
- 《机器学习(周志华)》 习题9.4参考答案
- Android Studio1.5使用和学习记录
- c++第六次循环
- Lowest Common Ancestor of a Binary Search Tree
- 面试题1:类的赋值运算符函数