美文网首页
iOS-UICollectionView基本使用

iOS-UICollectionView基本使用

作者: DeerRun | 来源:发表于2017-06-06 09:18 被阅读394次

UICollectionView

  • UICollectionView基于UIScrollView,系统默认内容自动偏移64各单位 ,所以可以通过设置以下设置取消偏移
    self.automaticallyAdjustsScrollViewInsets = NO;

  • UICollectionView必须设置瀑布流UICollectionViewFlowLayout,如:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 5.0;
    layout.minimumInteritemSpacing = 5.0;
    layout.sectionInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
  • UICollectionView必须设置数据源和代理,如
_mineCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64.0, DEVICE_WIDTH, DEVICE_HEIGHT - 64.0) collectionViewLayout:layout];
    _mineCollection.backgroundColor = [UIColor lightGrayColor];
    _mineCollection.dataSource = self;
    _mineCollection.delegate = self;
  • cell必须通过注册,如
    [_mineCollection registerClass:[MineCollectionViewCell class] forCellWithReuseIdentifier:collectionCell];
    [_mineCollection registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionHeader];
  • cell必须自定义,系统cell没有任何子控件
  • UICollectionView必须设置数据源和代理
//分区,组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

//每一分区的单元个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

//集合视图单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

//头部大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

//集合视图头部或者尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

//单元格复用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

//被选中的单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

UICollectionViewFlowLayout

  • 初始化layout后自动调动,可以在该方法中初始化一些自定义的变量参数

-(void)prepareLayout

  • Invalidate:刷新,在滚动的时候是否允许刷新布局
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}
  • 设置UICollectionView的内容大小,道理与UIScrollView的contentSize类似,@return 返回设置的UICollectionView的内容大小
-(CGSize)collectionViewContentSize
  • 初始Layout外观 @param rect 所有元素的布局属性 @return 所有元素的布局
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  • 根据不同的indexPath,给出布局 @param indexPath @return 布局
 -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

demo

相关文章

  • iOS-UICollectionView基本使用

    iOS-UICollectionView基本使用 iOS-UICollectionView基本使用

  • iOS-UICollectionView基本使用

    UICollectionView UICollectionView基于UIScrollView,系统默认内容自动偏...

  • iOS-UICollectionView

    CollectionView:UICollectionView、UITableView、NSCollectionV...

  • iOS-UICollectionView

    UICollectionView使用注意点 创建UICollectionView必须要有布局参数 cell必须通过...

  • iOS-UICollectionView入门

    一、UICollectionView介绍 UICollectionView和UICollectionViewCon...

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

网友评论

      本文标题:iOS-UICollectionView基本使用

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