UITableView

简介

  • 数据量大
  • 样式较为统一
  • 通常需要分组
  • 通常可视区只有一个,需要视图的复用

    结构

    UITableView.png

    UITableViewDataSource

  • UITableView作为视图,只负责展示,不管理数据

  • 需要开发者为UITableView提供数据及自定义样式时的UITableViewCell
  • 通过delegate模式实现UITableViewDataSource

    • @require
    • numberOfRowsInSection:(NSInteger) section;
    • cellForRowAtIndexPath:(NSIndexPath *) indexPath;

      UITableViewCell

      默认样式

      UITableViewCellStyle.png
      UITableViewCell.png

      cell重用

  • 系统提供复用回收池

  • 根据reuseIdentifier作为标识,从 tableView.dequeueReusableCellWithIdentifier中复用,如果没有再创建

UITableViewCell的重用.png

section

UITableViewSection.png

UITableViewDelegate

  • 提供滚动过程中,UITableViewCell的出现、消失时机
  • 提供UITableViewCell的高度、headers、footers设置
  • 提供UITableViewCell的各种行为的回调(点击、删除等)

    UICollectionView

    基础

    UICollectionView.png

    UICollectionViewDataSource

  • numberOfItemsInSection

  • cellForItemAtIndexPath

    UICollectionViewDelegate

  • willDisplayCell/endDisplayCell

  • didSelectItemAtIndexPath

    UICollectionViewCell

  • 不提供默认样式

    • 不是以行为设计基础
    • 只有contentView/backgroundView
    • 继承自UICollectionReusableView
  • 必须先注册Cell类型用于重用

    • forCellWithReuseIdentifier
    • dequeueReusableCellWithReuseIdentifier

      UICollectionViewLayout

      基础

  • UICollectionView提供基本的容器、滚动、复用功能,布局信息完全交给开发者

  • UICollectionViewLayout用于生成UICollectionView布局信息的抽象类,业务逻辑需要继承
  • 实现UICollectionViewLayout(UISubclassingHooks)中的方法
  • 开发者可自定义生成attribute,系统通过此进行布局
  • 系统提供默认的流失布局 UICollectionViewFlowLayout
    • 流式布局,每行排满后自动换行
    • minimumInteritemSpacing
    • minimumLineSpacing
    • itemSize

UICollectionViewFlowLayout.png

  • UICollectionViewDelegateFlowLayout根据indexPath做更细化的自定义样式 ```objectivec
  • (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.item % 3 == 0 ){
    1. return CGSizeMake(self.view.frame.size.width , 100);
    }else{
    1. return CGSizeMake((self.view.frame.size.width - 10 ) / 2 , 300);
    } } ```

    基本列表视图的选择和使用

    UICollectionView.png