zoukankan      html  css  js  c++  java
  • UICollectionView

    1 集合视图的创建  

    1.1 必须要先初始化一个 布局 

    self.layout = [[UICollectionViewFlowLayout alloc] init];

     (或者自己自定义的布局)

    1.2 再初始化集合视图本身

    self.collectionV = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.layout];

    1.3 可以设置集合视图布局的一些属性 如滚动方向 等

    self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;//垂直方向

    2 设置集合视图的 其他属性  代理的方法实现

    2.1 首先接受协议 数据源协议和代理  

    <UICollectionViewDataSource,UICollectionViewDelegate>

    2.2 接受两个协议

     self.collectionV.dataSource = self;//数据源代理     self.collectionV.delegate = self;//协议代理

    2.3 协议中有必须实现的两个方法(集合视图的每个分区的item的个数,和 cell的 设置)

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;33

    2.4 创建自定义的cell 首先需要注册 

    [self.collectionV registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    3 需要设置 集合视图的布局属性

    3.1//设置分区的个数

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    3.2 设置代理里的方法 集合视图的行宽 列宽

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;//每个item的大小

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;//设置行宽

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;//设置列宽

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;//上左下右的尺寸

    3.3 还可以设置 每个item点击执行的方法

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

    4 设置 集合视图的增广视图

    4.1 首先需要创建一个自定义的增广视图

    UICollectionReusableView//继承自这个类

    4.2 设置 增广视图的方法

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

    4.3 设置增广视图的尺寸

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

    4.4 需要注册增广视图的方法

    [self.collectionV registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    总结:collectionView 集合视图的使用 有三个协议,其中布局的协议 集合视图的代理协议(delegate)代理包含了指定代理人;列外 注册cell和增广视图的方法不一样,应该注意;

    还有在自定义增广视图是,设置内部的控件的frame是注意,尺寸的选取,应该是参考 self.bounds。。

  • 相关阅读:
    hdu1238 Substrings
    CCF试题:高速公路(Targin)
    hdu 1269 迷宫城堡(Targin算法)
    hdu 1253 胜利大逃亡
    NYOJ 55 懒省事的小明
    HDU 1024 Max Sum Plus Plus
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1257 最少拦截系统
    HDU 1069 Monkey and Banana
    HDU 1104 Remainder
  • 原文地址:https://www.cnblogs.com/jiurong001/p/5175021.html
Copyright © 2011-2022 走看看