UICollectionView和UITableView很相似的一個UI控件,可是UICollectionView強大很多! 下面我們來認識一下UICollectionView相關的一下知識:
UICollectionView的數據源:和UITableView類似
// 分組,不實現該方法,則默認為一組
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
// 每組item的個數
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3;
}
UICollectionView常用的代理方法:
//定義item的屬性;注意:這里和UITableView有點區別,UITableView可以從復用池中取出cell,也可以重新創建一個cell;而UICollectionView的item只能從復用池中取出;必須先注冊才可以使用,header和footer也是一樣;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
}
// item是否可以移動
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 移動item時調用
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
// 是否允許選中某個item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//選中時觸發
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
//選中其他的item時 是否允許取消選中狀態
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 取消選中狀態時觸發
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
}
// 點擊item時是否允許高亮狀態
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 高亮狀態時調用
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
}
// 高亮狀態結束時調用
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
}
// 長按時是否顯示菜單
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 是否展示菜單的選項:cut:、copy:、select:、selectAll:、paste:、delete:等選項;其中只有cut:、copy:、paste:在這可以使用;
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
return YES;
}
// 選擇菜單的某個選項后調用,進行處理
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
NSLog(@"%@",NSStringFromSelector(action));
}
// 是否允許移動item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// item移動的時候調用:sourceIndexPath移動前,destinationIndexPath移動后
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
[self.shopArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
}
// 實現collectionView頭部或者是底部的設置;前提是注冊了頭部或者是底部并且設置了size
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *view = [ collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
view.backgroundColor = [UIColor redColor];
return view;
}
//注冊item
[collectionView registerClass:[YANCollectionCell class] forCellWithReuseIdentifier:ID];
// 注冊header
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
// 注冊footer
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer"];
UICollectionViewFlowLayout的基本使用
UICollectionViewFlowLayout是UICollectionViewLayout的子類;官方封裝好的,可以直接使用;如果想實現像瀑布流那樣的布局,就得自己封裝UICollectionViewLayout啦!
基本設置:
minimumLineSpacing // 最小行間距
minimumInteritemSpacing // 最小列間距
scrollDirection //布局樣式:UICollectionViewScrollDirectionVertical、UICollectionViewScrollDirectionHorizontal
itemSize //item大小
UICollectionViewDelegateFlowLayout代理方法:
// 設置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(80, 90);
}
// 設置分組的collectionView的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(20, 10, 40, 50);
}
// 設置最小行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 100;
}
//最小列間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 50;
}
// 設置header的size;實現這個代理的方法的前提是注冊了header
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(30, 80);
}
// 設置footer的size; 實現這個代理的方法的前提是注冊了footer
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
return CGSizeMake(50, 100);
}
實現UICollectionView的item移動:
- 給UICollectionView添加一個手勢;(具體什么手勢根據需求確定,我這里是長按手勢)
- 根據手勢判斷item的位置,并實現代理方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
修改數據源;
- (void)longGestureRecog:(UILongPressGestureRecognizer *)longGesture{
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:{
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:{
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
}
break;
case UIGestureRecognizerStateEnded:{
[self.collectionView endInteractiveMovement];
}
break;
default:{
[self.collectionView cancelInteractiveMovement];
}
break;
}
}
UICollectionViewLayout的基本使用
//類似于- (void)layoutSubviews;實現頁面布局;
- (void)prepareLayout{
[super prepareLayout];
}
//設置UICollectionView的 ContentSize,影響cell是否顯示完全的問題;
- (CGSize)collectionViewContentSize{
}
//設置item的UICollectionViewLayoutAttributes;即cell的bounds,size,transform,alpha等一些屬性的設置;
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath* )indexPath{
}
//獲取可視范圍內的cell的UICollectionViewLayoutAttributes屬性;
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
}
//布局發生改變時,是否重新加載,重新執行- (void)prepareLayout;
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
}
//獲取UICollectionView的最終偏移量;
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
}