• UItableView与UICollectionView


    UITableView

    1. UITableViewStyleGrouped 分区表格样式创建表格

       .separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;  // 雕刻,双线效果。 测试无效

    2. UITableView的其他方法属性

        // 表格是否进入编辑模式 ,用以显示添加,删除,移动标识

        setEditing:   // 添加,删除不能同时显示

        .allowsSelectionDuringEditing   // 在编辑模式下是否允许可点击选择

        // 返回编辑模式下,每行左侧显示的是删除还是添加

        - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

         //  在可处理删除/添加方法中判断每行是何种操作 在tableviewdatasource协议下

        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

        //  行被移动后处理方法 在tableviewdatasource协议下

        - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

        //  自ios8开始支持的:在每行左滑或者编辑模式下点左侧删除按钮,右侧滑出多个按钮

        - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

    3.  折叠分区内容

         在分区样式下设置多个分区,每个分区对应不同的数据源

    4.  UITableViewCell 定制

         定制分为三种方式:纯代码,xib,storyboard

         > 纯代码

         -自定义类继承自UITableViewCell

         -添加子视图控件属性,以使其可通过cell.方式可获取

         -重写父类方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  // 复用时单元格初始化调用此方法,在其内实现初始化子视图,将子视图添加到cell.contentView中

    /*** 开始- UITableViewCell 自适应内容高度 ***/

          对于纯文字的情况(若还有其他,如图片,高度累加即可)

          - 计算文字在给定宽度下所占用的尺寸

              # 设定或获取文字的属性

              # 计算在给定文字属性,绘制模式下,文字的高度

                  [string boundingRectWithSize:CGSizeMake(300, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]

          - 在返回行高协议方法中,返回计算的高度,根据实际情况可累加固定值

          - 在绑定单元格内容的协议方法中,获取计算的高度,并通过此设置子视图的尺寸

    /*** 结束- UITableViewCell 自适应内容高度 ***/

         > xib 定制cell

         - 自定类继承自UITableViewCell 并选择创建xib文件的选项

         - 拖曳视图控件到xib并创建外联属性与其关联

         - 选择此cell, Xcode右侧属性窗口下Table View Cell下设置 Identifier, 用于单元格复用的标识

         - 若使用方式二复用,则注册时,选择registerNib:forCellReuseIdentifier: 

         - 其余步骤与之前基本一致

         > storyboard 定制cell

    UICollectionView

    1. UICollectionView

       网格视图,除了提供与UITableView同样的功能显示一组顺序显示的数据,还支持多列的布局。

    2. UICollectionView 使用

        > 设置Controller要遵循的协议:

        UICollectionViewDataSource               // 数据源协议

        UICollectionViewDelegate                   // 数据行为协议

        UICollectionViewDelegateFlowLayout   // 数据流式布局协议

        > 创建流式布局

        flowLayout = [[UICollectionViewFlowLayout alloc] init]

        > 设置该布局下滑动方向

        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical / Horizontal  // 默认是Vertical 在Horizontal时,元素是依次竖向摆放,Vertical时,依次横向摆放

         > 以布局方式和frame 初始化UICollectionView

         [[UICollectionView alloc] initWithFrame: collectionViewLayout: ]

         > 设置背景色,纯代码实现时,UICollectionView默认背景色是黑色

         .barckgroundColor

         > 设置代理

         .dataSource     // 代理 UICollectionViewDataSource

         .delegate         // 代理 UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout

         > 注册标识单元格UICollectionViewCellUICollectionView Cell只能使用定制化的,并且必须注册。

         [collectionView registerNib: forCellWithReuseIdentifier:]

         [collectionView registerClass: forCellWithReuseIdentifier:]

         > 注册标识分区表头/表尾 UICollectionReusableView,创建时要手动设置其class

         [collectionView registerNib: forSupplementaryViewOfKind: withReuseIdentifier: ]

         /* forSupplementaryViewOfKind: 表示是为表头还是表尾注册,参数只有两个可选值:UICollectionElementKindSectionHeader:表头,UICollectionElementKindSectionFooter: 表尾  */

         > 协议方法

         - UICollectionViewDataSource

         必须实现:

    // 每个分区条数(不是行数)

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

           // 数据绑定cell

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

          其他方法:

    // 返回分区数量

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;      

      // 数据绑定表头/表尾:必须为其设置尺寸才可显示,注意查看不同scrollDirection下表头的展现

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

         - UICollectionViewDelegateFlowLayout

      // 返回分区表头尺寸

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

         // 返回分区表尾尺寸

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:

    (UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

         // 返回每条的尺寸

         - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

         // 返回每个分区四周的外间距

         - (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

        -UICollectionViewDelegate

       // 条被选中:注意被选中的背景色,只能自定义selectedBackgroundView,且设置后,不能设置cell的背景色,否则无法看到点击选中效果

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

      // 条被反选

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

     

  • 相关阅读:
    骑士飞行棋第二版(已完善)
    骑士飞行棋第一版(掷骰子方法分开)
    do -while语句的使用
    随机数例子(Random)
    Day6_time模块
    Day6_正则表达式
    Day6_内置函数
    centos6-7克隆主机网卡无法启动问题
    rsync+innotify做到同步式更新
    apache压力测试工具的apache bench和JMeter的安装
  • 原文地址:https://www.cnblogs.com/ljcgood66/p/5538863.html
Copyright © 2020-2023  润新知