关于UICollectionView一些笔记
来源:互联网 发布:mac cad怎么样卸载软件 编辑:程序博客网 时间:2024/05/19 16:21
UICollectionView
http://code4app.com/forum.php?mod=viewthread&tid=9339&highlight=collectionview
一.什么是UICollectionView
它和UItableView类似,可以做九宫格布局的一种view。
二.如何创建UICollectionView
1.遵守协议
UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
2.创建数据源
3.创建UI
1)设置布局
UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc] init];
//设置cell之间的上下边距
layout.minimumLineSpacing = 10;
//设置cell之间的左右边距
layout.minimumInteritemSpacing = 10;
//设置collectionView滚动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置cell的大小 itemSize代表cell的大小
// layout.itemSize = CGSizeMake(110, 150);
2)将layout传入UICollectionView中实现初始化
_collectonView= [[UICollectionView alloc] initWithFrame:self.view.boundscollectionViewLayout:layout];
_collectonView.backgroundColor = [UIColorwhiteColor];
//外观
_collectonView. delegate = self;
//数据源
_collectonView.dataSource = self;
_collectonView.showsVerticalScrollIndicator = NO;
#pragmamark --- UICollectionViewDelegate
//设置段个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
return self.dataArrM.count;
}
//设置每一段cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionViewnumberOfItemsInSection:(NSInteger)section
{
Group *group = self.dataArrM[section];
return group.photoArrM.count;
}
//设置cell
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
Group *group = self.dataArrM[indexPath.section];
Photo *p1 = group.photoArrM[indexPath.row];
cell.imgView.image = [UIImage imageNamed:p1.photoName];
cell.nameLabel.text = p1.name;
return cell;
}
//设置cell的偏移量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section
{
//上 左 下 右 控制cell的位置
return UIEdgeInsetsMake(10, 12, 10, 12);
}
//设置cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(200, 150);
}
//设置段头高度
-(CGSize)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout *)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(375, 25);
}
//设置段头
- (UICollectionReusableView *)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString *)kindatIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
UICollectionReusableView *headView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"head" forIndexPath:indexPath];
Group *group = self.dataArrM[indexPath.section];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375,25)];
label.textAlignment =NSTextAlignmentCenter;
label.text = group.groupName;
label.backgroundColor = [UIColor orangeColor];
label.tag = 100 + indexPath.section;
[headView addSubview:label];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(delete:)];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tap];
return headView;
}else
{
}
return nil;
}
- (void)delete:(UITapGestureRecognizer*)tap
{
[self.dataArrM removeObjectAtIndex:tap.view.tag - 100];
[_collectonView reloadData];
}
//点击cell触发
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
Group *group = self.dataArrM[indexPath.section];
Photo *p1 = group.photoArrM[indexPath.row];
//传图片名字
NSLog(@"点击了%@",p1.name);
}
//是否允许点击cell
- (BOOL)collectionView:(UICollectionView*)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 1) {
return NO;
}
}
return YES;
}
注意:Cell和段头和段尾都要注册
要调用要先注册
- (void)viewDidLoad {
//注册手写cell
[_collectonView registerClass:[CustomCell class]forCellWithReuseIdentifier:@"cell"];
//手写注册cell段头
[_collectonView registerClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
//手写注册cell段尾
[_collectonViewregisterClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"];
}
- 关于UICollectionView一些笔记
- UICollectionView 笔记
- 关于 UICollectionView 的一些代理和数据源方法
- 21.关于使用UICollectionView的一些小技巧
- UICollectionView的一些属性
- UICollectionView 的一些坑
- UICollectionView的一些常见问题
- 创建 UICollectionView 对象(UICollectionView一些知识)
- 关于集合视图UICollectionView
- 关于UICollectionView的学习
- 关于UICollectionView的使用
- UICollectionView笔记一:BASIC
- UICollectionView笔记-1
- UICollectionView笔记-2
- UICollectionView使用笔记
- 归纳笔记010:UICollectionView
- iOS UICollectionView笔记
- UICollectionView笔记1
- 用美图秀秀(微软调色版)生成的R分量为255的图片在MATLAB和OpenCV中R分量为254
- 数据库了解及MySQL学习(持续更新)
- 字符串MD5加密
- centos安装nginx全过程 备份
- mysql索引总结----mysql 索引类型以及创建
- 关于UICollectionView一些笔记
- [知识整理]Linux系统WIFI知识的一些整理
- Android开发——Intent中的各种FLAG
- Spring管理 hibernate 事务配置的五种方式
- 今年暑假不AC
- Message,MessageQueue,Looper,Handler详解+实例
- linux /proc文件系统解析
- iOS 动画大全(附带gif图片效果)
- 互联网推送服务原理:长连接+心跳机制(MQTT协议)