UIcollectionView基础详解


简介:UIcollectionView是我们在做iOS高级视图中经常用到的控件,他的特点就是非常灵活。只要有算法支撑,对于界面布局来说塔五说不能。

1.对比学习:

UIcollectionView还有两个看是兄弟的控件。NSCollectionView和UITableView,但是他们绝逼不能等效。

2.UIcollectionView的特点介绍:

(1)他可以高度制定内容的展现

(2)能够高效处理大量数据

3.UIcollectionView的构成元素

(1)Cell(单元格)

(2)Supplementary Views (补充视图【我们也可以对比tableView,类似于TableView的页眉和页脚】)

(3)Decoration Views (装饰视图,用于装饰整个UIcollectionView)

如下图所示:

 

4.UIcollectionView代理介绍:

(1)UICollectionViewDataSource:(用于为Collection提供数据)

 主要功能:

  【1】Section数目

  【2】Section里面有多少item

  【3】提供Cell和supplementary view(补充视图)设置

  方法(文档):

 1 @protocol UICollectionViewDataSource <NSObject>
 2 @required
 3 //Section个数
 4 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
 5 //设置Cell
 6 // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
 7 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
 8 
 9 @optional
10 //每个section中item的个数
11 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
12 
13 // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:对头视图或者尾视图进行设置
14 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
15 //设置某个item是否可以被移动,返回NO则不能移动
16 - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
17 - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
18 //移动item的时候,会调用这个方法
19 @end
 1 @protocol UICollectionViewDelegate <UIScrollViewDelegate>
 2 @optional
 3 
 4 // Methods for notification of selection/deselection and highlight/unhighlight events.
 5 // The sequence of calls leading to selection from a user touch is:
 6 //
 7 // (when the touch begins)
 8 // 1. -collectionView:shouldHighlightItemAtIndexPath:
 9 // 2. -collectionView:didHighlightItemAtIndexPath:
10 //
11 // (when the touch lifts)
12 // 3. -collectionView:shouldSelectItemAtIndexPath: or -collectionView:shouldDeselectItemAtIndexPath:
13 // 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
14 // 5. -collectionView:didUnhighlightItemAtIndexPath:
//这个协议用来设置和处理collectionView的功能和一些逻辑,所有方法都是可选实现:是否允许某个Item的高亮,返回NO,则不能进入高亮状态
15 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
//当item高亮时触发的方法
16 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
//结束高亮状态时触发的方法
17 - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
//是否可以选中某个Item,返回NO,则不能选中
18 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//是否可以取消选中某个Item
19 - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode
//已经选中某个item时触发的方法 20 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//取消选中某个Item时触发的方法
21 - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath; 22 //将要加载某个Item时调用的方法 23 - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
//将要加载头尾视图时调用的方法
24 - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
//已经展示某个Item时触发的方法
25 - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
//已经展示某个头尾视图时触发的方法
26 - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath; 27 28 // These methods provide support for copy/paste actions on cells. 29 // All three should be implemented if any are.
//这个方法设置是否展示长按菜单
30 - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
//这个方法用于设置要展示的菜单选项
31 - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender;
//这个方法用于实现点击菜单按钮后的触发方法,通过测试,只有copy,cut和paste三个方法可以使用
32 - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender; 33 34 // support for custom transition layout
//collectionView进行重新布局时调用的方法 35 - (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout; 36 37 // Focus
//焦点 38 - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); 39 - (BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0); 40 - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0); 41 - (nullable NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView NS_AVAILABLE_IOS(9_0); 42 43 - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath NS_AVAILABLE_IOS(9_0); 44 45 - (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset NS_AVAILABLE_IOS(9_0); // customize the content offset to be applied during transition or update animations 46 47 @end

4. iOS6之后,Cell重用得到改善

我们可以更加方便的使用Cell,系统总是为我们初始化Cell。我们可以直接使用。只需要简单的按照两步走即可:

(1)必须使用下面方法对Cell进行Cell类的注册:

 

    

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM