iOS UICollectionView: The Complete Guide摘要
2015-09-22 17:20
579 查看
本文的内容来自iOS UICollectionView: The Complete Guide, Second Edition。文章对应的源码可以再http://ashfurrow.com/uicollectionview-the-complete-guide/处下载。
backgroundView如果被设置,就会永久的显示。当cell被selected,selectedBackgroundView就会被添加到view的层级中,当cell变成unselected,它就会被移除。
下面通过例子说明。通过如下代码来允许多选:
通过设置selectedBackgroundView来设置selected的背景view。重写setHighlighted来设置highlighted的样式。自定义的UICollectionViewCell如下:
运行的效果如下:
Supplementary views的显示的内容由数据源提供,但由
内置的
Supplementary views的行为与cell类似,collection view注册一个
如下,注册一个
除此之外,还需要给Supplementary views指定一个大小
如下,通过flow layout的
需要注意的是:当水平滚动时,使用的是
之后,实现
选择cell
修改collection view的内容
自定义selection行为
支持Cut/Copy/Paste
如下的例子,使
1.使
2.
如下,支持copy
3.执行action对应的操作
如下,执行copy操作
iOS UICollectionView: The Complete Guide摘要
基本
selected、highlighted的理解
cell有两个重要的bool属性:selected和highlighted。highlighted完全由用户的交互决定,当用户的手指按下一个cell时,它就自动的变成highlighted。如果cell支持selection,当用户抬起他的手指,cell就变成selected。cell会保持selected,直到你写的一些代码使它unselected或者用户再次点击它。UICollectionViewCell的层级关系如下所示:backgroundView如果被设置,就会永久的显示。当cell被selected,selectedBackgroundView就会被添加到view的层级中,当cell变成unselected,它就会被移除。
下面通过例子说明。通过如下代码来允许多选:
self.collectionView.allowsMultipleSelection = YES;
通过设置selectedBackgroundView来设置selected的背景view。重写setHighlighted来设置highlighted的样式。自定义的UICollectionViewCell如下:
@interface AFCollectionViewCell : UICollectionViewCell @property (nonatomic, strong) UIImage *image; @end @implementation AFCollectionViewCell { UIImageView *imageView; } - (id)initWithFrame:(CGRect)frame { if (!(self = [super initWithFrame:frame])) return nil; self.backgroundColor = [UIColor whiteColor]; imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, 10, 10)]; [self.contentView addSubview:imageView]; UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.8f]; self.selectedBackgroundView = selectedBackgroundView; return self; } #pragma mark - Overriden UICollectionViewCell methods -(void)prepareForReuse { [super prepareForReuse]; self.backgroundColor = [UIColor whiteColor]; self.image = nil; //also resets imageView’s image } -(void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (self.highlighted) { imageView.alpha = 0.8f; } else { imageView.alpha = 1.0f; } } #pragma mark - Overridden Properties -(void)setImage:(UIImage *)image { _image = image; imageView.image = image; }
运行的效果如下:
Supplementary Views
Supplementary views 是指和cell一起滚动并显示某些信息的viewSupplementary views的显示的内容由数据源提供,但由
UICollectionViewLayout对象布局
内置的
UICollectionViewFlowLayout提供了两种类型的Supplementary views:header和footer。
Supplementary views的行为与cell类似,collection view注册一个
Class或者
UINib,Supplementary views会被复用
如下,注册一个
header:
[surveyCollectionView registerClass:[AFCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderIdentifier];
除此之外,还需要给Supplementary views指定一个大小
如下,通过flow layout的
headerReferenceSize指定header大小:
surveyFlowLayout.headerReferenceSize = CGSizeMake(60, 50);
需要注意的是:当水平滚动时,使用的是
CGSize的
width,而header的垂直方向会被拉伸,填满整个空间。当垂直滚动时,使用的是
CGSize的
height,header水平方向填满整个区域,示意如下:
之后,实现
UICollectionViewDelegate的
collectionView:viewForSupplementaryElementOfKind: atIndexPath:方法,即可显示Supplementary views ,如下:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { //Provides a view for the headers in the collection view AFCollectionHeaderView *headerView = (AFCollectionHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderIdentifier forIndexPath:indexPath]; ...... }
用户交互
一些常用的代理方法:选择cell
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
修改collection view的内容
insertSections: deleteSections: reloadSections: moveSection:toSection: insertItemsAtIndexPaths: deleteItemsAtIndexPaths: reloadItemsAtIndexPaths: moveItemAtIndexPath:toIndexPath:
自定义selection行为
collectionView:shouldHighlightItemAtIndexPath: collectionView:shouldSelectItemAtIndexPath: collectionView:shouldDeselectItemAtIndexPath:
支持Cut/Copy/Paste
如下的例子,使
collection view的cell支持
copy
1.使
collection view支持menu
-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath { return YES; }
2.
collection view会询问代理对象,支持的action的类型。目前有
cut:
copy:和
paste:
如下,支持copy
-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { if ([NSStringFromSelector(action) isEqualToString:@"copy:"]) { return YES; } return NO; }
3.执行action对应的操作
如下,执行copy操作
-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { if ([NSStringFromSelector(action) isEqualToString:@"copy:"]) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:[[self photoModelForIndexPath:indexPath] name]]; } }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Rails Routes中new、collection、member的区别浅析
- 解决 The Controls collection cannot be modified because the control contains code blocks
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- codeigniter中view通过循环显示数组数据的方法
- MVVM模式中ViewModel和View、Model有什么区别?
- android开发教程之view组件添加边框示例
- Android动态添加View的问题解决方法
- 探讨Android与iOS,我们将何去何从?
- Android中将View的内容保存为图像的简单实例
- Android View添加 Listener 实例代码
- Android加载View中Background详解