美文网首页UI的实现
UICollectionView常用属性及方法

UICollectionView常用属性及方法

作者: YaYoYo | 来源:发表于2017-07-12 19:36 被阅读146次
    不常用属性

    //默认选中第一个collectionViewCell

    - (void)viewDidAppear:(BOOL)animated {
        NSInteger selectedIndex = 0;
        
        NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        
        [self.pageCollection selectItemAtIndexPath:selectedIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        //selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        
        [super viewDidAppear:animated];
    }
    

    -. 布局

    UICollectionViewFlowLayout流水布局

    1. 通过UICollectionViewFlowLayout对象进行布局( 如果布局的尺寸是固定的,例如:item的尺寸大小都是固定,可以使用全局属性,即设置下文中layout )
      UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    2. // 1.设置列间距
      3. layout.minimumInteritemSpacing = 1;
    3. // 2.设置行间距
      5. layout.minimumLineSpacing = 1;
    4. // 3.设置每个item的大小
      layout.itemSize = CGSizeMake(50, 50);
    5. // 4.设置Item的估计大小,用于动态设置item的大小,结合自动布局(self-sizing-cell)
      layout.estimatedItemSize = CGSizeMake(320, 60);
    6. // 5.设置布局方向
    UICollectionViewScrollDirectionVertical垂直(竖着布局)
    UICollectionViewScrollDirectionHorizontal水平(横向布局)```
    ###约束属性
     设置头视图尺寸大小
     ` layout.headerReferenceSize = CGSizeMake(50, 50);`
    设置尾视图尺寸大小
    `  layout.footerReferenceSize = CGSizeMake(50, 50);`
     设置分区(组)的EdgeInset(四边距)
    `layout.sectionInset = UIEdgeInsetsMake(10, 20, 30, 40);`
    设置分区的头视图和尾视图是否始终固定在屏幕上边和下边
    `layout.sectionFootersPinToVisibleBounds = YES;`
    `layout.sectionHeadersPinToVisibleBounds = YES;`
    /** 注册collectionView cell 的重用池*/
      ``` [_homePagecollection registerNib:[UINib nibWithNibName:@"HomePageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:registert];```
    /** 注册collectionView 头视图的重用池*/
     ` [_homePagecollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];`
    
     通过遵守UICollectionViewDelegateFlowLayout实现代理方法来布局(非固定情况则需要通过数据源方法)
    //动态设置每个分区的EdgeInsets
    ```- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;```
    
    //动态设置每行的间距大小
    ```- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;```
    
    //动态设置每列的间距大小
    ```- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;```
    
    //动态设置某组头视图大小
    ```- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;```
    
    //动态设置某组尾视图大小
    ```- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;```
    二. 数据源方法
    //设置分区数(必须实现)
    ```- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }```
    
    //设置每个分区的item个数
    ```- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
        return 5;  
    }```
    
    //设置返回每个item的属性必须实现)
    ```- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }```
    
    //对头视图或者尾视图进行设置
    ```- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        NSString *identifier;
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            identifier = @"headerView";
        } else {
            identifier = @"footerView";
        }
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:identifier forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 120, 30)];
        [view addSubview:label];
        if (indexPath.section == 0) {
            label.text = @"section1";
        }else {
            label.text = @"section2";
        }
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            view.backgroundColor = [UIColor redColor];
        } else {
            view.backgroundColor = [UIColor purpleColor];
        }
        return view;
    }```
    
    //是否允许移动Item
    ```- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0){
        return YES;
    }```
    
    //移动Item时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0); {
    }```
    三. 常用到的代理方法
    //是否允许某个Item的高亮,返回NO,则不能进入高亮状态
    ```- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath; {
        return YES;
    }```
    
    //当item高亮时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //结束高亮状态时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //是否可以选中某个Item,返回NO,则不能选中
    ```- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath; {
        return YES;
    }```
    
    //是否可以取消选中某个Item
    ```- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; {
        return YES;
    }```
    
    //已经选中某个item时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //取消选中某个Item时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //将要加载某个Item时调用的方法
    ```- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); {
    }```
    
    //将要加载头尾视图时调用的方法
    ```- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); {
    }```
    
    //已经展示某个Item时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //已经展示某个头尾视图时触发的方法
    ```- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath; {
    }```
    
    //这个方法设置是否展示长按菜单
    ```- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath; {
        return YES;
    }```
    
    //这个方法用于设置要展示的菜单选项
    ```- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender; {
        return YES;
    }```
    
    //这个方法用于实现点击菜单按钮后的触发方法,通过测试,只有copy,cut和paste三个方法可以使用
    ```- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender; {
        //通过下面的方式可以将点击按钮的方法名打印出来:
        NSLog(@"%@",NSStringFromSelector(action));
    }```
    
    //collectionView进行重新布局时调用的方法
    ```- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout; {
    }```
    

    相关文章

      网友评论

        本文标题:UICollectionView常用属性及方法

        本文链接:https://www.haomeiwen.com/subject/euxihxtx.html