簡介: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類的注冊: