概述


  • 实现效果

  • 设计思路

    • 采用MVC架构,即模型—视图-控制器架构
    • 使用MJExtension框架实现字典转模型
    • 使用MJRefresh框架实现上拉和下拉刷新

      • 上拉刷新,加载新的数据
      • 下拉刷新,加载更多的数据
    • 使用SDWebImage框架加载图片

模型


  • 商品模型需要包含以下属性

    /**商品控件宽度*/
    @property (nonatomic, assign) CGFloat w;
    /**商品控件高度*/
    @property (nonatomic, assign) CGFloat h;
    /**商品图片*/
    @property (nonatomic, copy) NSString *img;
    /**商品价格*/
    @property (nonatomic, copy) NSString *price;

视图


  • 通过xib来实现自定义cell(继承自UICollectionViewCell),xib的结构如图

  • 视图的代码实现

    • 包含商品模型属性

      /**商品模型*/
      @property (nonatomic, strong) LYPShop *shop;
    • 引用xib中的控件

      //展示商品图片
      @property (weak, nonatomic) IBOutlet UIImageView *imageView;
      //显示商品价格
      @property (weak, nonatomic) IBOutlet UILabel *priceLabel;
    • 重写商品模型的setter,使xib中的控件显示乡音的内容

      - (void)setShop:(LYPShop *)shop
      {
      _shop = shop; //使用SDWebImage设置商品图片
      [self.imageView sd_setImageWithURL:[NSURL URLWithString:shop.img] placeholderImage:[UIImage imageNamed:@"loading"]];
      //设置商品价格
      self.priceLabel.text = shop.price;
      }

控制器


  • 创建展示商品的容器,即UICollectionView对象,并初始化

    - (void)setupCollectionView
    {
    //通过封装的自定义布局,创建布局
    LYPWaterFlowLayout *layout = [[LYPWaterFlowLayout alloc] init]; //设置layout的代理
    layout.delegate = self; //创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView = collectionView; //设置数据源
    collectionView.dataSource = self;
    //将collectionView添加到控制器的view中
    [self.view addSubview:collectionView];
    }
  • 注册通过xib自定义的cell

    /**设置cell的重用标示*/
    static NSString *const LYPShopID = @"shop";
    - (void)registerCell
    {
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LYPShopCell class]) bundle:nil] forCellWithReuseIdentifier:LYPShopID];
    }
  • 设置商品模型数组

    • 添加成员属性

      /**所有的商品模型数组*/
      @property (nonatomic, strong) NSMutableArray *shops;
    • 通过懒加载的方式,初始化模型数组

      - (NSMutableArray *)shops
      {
      if (_shops == nil)
      {
      _shops = [NSMutableArray array];
      }
      return _shops;
      }
  • 实现刷新功能

    • 刷新的业务逻辑,如图

    • 设置上拉刷新和下拉刷新控件

      - (void)setupRefresh
      {
      //下拉刷新控件
      self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
      //view加载完毕,开始下拉刷新
      [self.collectionView.header beginRefreshing]; //上拉刷新控件
      self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
      //一开始隐藏footer
      self.collectionView.footer.hidden = YES;
      }
    • 实现加载数据功能

      /**下拉刷新*/
      - (void)loadNewShops
      {
      //通过该函数模拟网络延迟
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //将字典转成模型
      NSArray *shops = [LYPShop objectArrayWithFilename:@"1.plist"]; //清空之前所有的商品模型信息
      [self.shops removeAllObjects];
      //将最新的商品模型信息添加到模型数组中
      [self.shops addObjectsFromArray:shops]; //刷新数据
      [self.collectionView reloadData];
      //结束刷新
      [self.collectionView.header endRefreshing];
      });
      }
      /**上拉刷新*/
      - (void)loadMoreShops
      {
      //通过该函数模拟网络延迟
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      //通过该函数模拟网络延迟
      NSArray *shops = [LYPShop objectArrayWithFilename:@"1.plist"];
      //将新加载的商品模型信息添加到模型数组中
      [self.shops addObjectsFromArray:shops]; //刷新数据
      [self.collectionView reloadData];
      //结束刷新
      [self.collectionView.footer endRefreshing];
      });
      }
  • 设置collectionView的数据源

    • 遵守协议UICollectionViewDataSource
    • 设置cell的个数

      - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
      {
      //设置上拉刷新控件的状态,无商品时不显示
      self.collectionView.footer.hidden = self.shops.count == 0;
      //返回cell的个数
      return self.shops.count;
      }
    • 创建indexPath位置的cell

      - (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
      {
      //通过重用标示从缓存池中取,若取不到,则自动创建
      LYPShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LYPShopID forIndexPath:indexPath]; //给视图cell的模型赋值,使其设置cell中控件的显示内容
      cell.shop = self.shops[indexPath.item]; return cell;
      }
  • 实现layout的代理方法,定制布局

    • 设置每个cell的高度

      - (CGFloat)waterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout heightForItemAtIndex:(NSInteger)index itemWith:(CGFloat)itemWith
      {
      LYPShop *shop = self.shops[index];
      //通过比例计算cell的高度
      return itemWith * shop.h / shop.w;
      }
    • 设置列数

      - (NSInteger)columnCountInWaterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout
      {
      return 2;
      }
    • 设置行间距

      - (CGFloat)rowMarginInWaterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout
      {
      return 20;
      }
    • 设置列间距

      - (CGFloat)columnMarginInWaterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout
      {
      return 40;
      }
    • 设置内边距

      - (UIEdgeInsets)edgeInsetsInWaterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout
      {
      return UIEdgeInsetsMake(20, 10, 10, 30);
      }

改变布局


  • 可以简单地通过改变返回列数的代理方法,来改变布局

    - (NSInteger)columnCountInWaterFlowLayout:(LYPWaterFlowLayout *)waterFlowLayout
    {
    //返回3列
    return 3;
    }
  • 效果如图

 
 

OC - 31.通过封装的自定义布局快速实现商品展示的更多相关文章

  1. OC - 30.如何封装自定义布局

    概述 对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果 由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可 在进行分装的时候,通常需要用 ...

  2. OC - 29.自定义布局实现瀑布流

    概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...

  3. Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面

    网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...

  4. Collection View 自定义布局(custom flow layout)

    Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...

  5. 使用Style自定义ListView快速滑动图标

    一.显示ListView快速滑动块图标 设想这样一个场景,当ListView的内容有大于100页的情况下,如果想滑动到第80页,用手指滑动到指定位置,无疑是一件很费时的事情,如果想快速滑动到指定的位置 ...

  6. AcitonBar 自定义布局

    Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 自定义Activity主题和ActionBar样式 在新建的 ...

  7. Xamarin自定义布局系列——瀑布流布局

    Xamarin.Forms以Xamarin.Android和Xamarin.iOS等为基础,自己实现了一整套比较完整的UI框架,包含了绝大多数常用的控件,如下图 虽然XF(Xamarin.Forms简 ...

  8. Android 自定义支持快速搜索筛选的选择控件(一)

    Android 自定义支持快速搜索筛选的选择控件 项目中遇到选择控件选项过多,需要快速查找匹配的情况. 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz ...

  9. Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中的输入流 第一节课

    Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig  CentOS远程连接  Linux中 ...

随机推荐

  1. 机器安装第二个tomcat ,出现报错如何解决

    1.本机安装第二个 tomcat 后,出现 报错如下图所示 最后解决办法 是 在安装的时候  ,windows 服务名称 和 另一个tomcat  起不一样的 名称就可以了 如下图

  2. Linq中小心使用IndexOf

      我们平常在做字符串的模糊查询时,有可能会用到下面的类似LINQ写法: string.IsNullOrEmpty(_SN) ? true : a.SN.IndexOf(_SN) != -1   这条 ...

  3. Eclipse的下载和安装

    下载 Android开发首选Eclipse for Android Developers版本,里面集成了ADT(Android Development Tools). 下载页面:http://www. ...

  4. JavaScript 概览 更新时间2014-0414-0837

    一些概念 DOM(文档对象模型)是HTML和XML的应用程序接口(API).DOM Level1规划文档结构:DOM Level2扩展了对鼠标和用户界面事件等的支持:DOM Level3支持了XML1 ...

  5. ACM3787

    /* 问题说明 给定两个整数A和B,其表示形式是:从个位开始, 每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入 输入包含多组数据数据,每组数据占一行, ...

  6. 通过例子学python(2.2)

    2.2 通用序列操作 #2.2 通用序列操作 #所有序列类型都可以进行的操作:索引indexing,分片sliceing,加adding,乘multiplying,成员资格, #计算序列长度,找出最大 ...

  7. Counting - SGU 117(快速幂)

    题目大意:求下面N个数里面有多少个数的M次方能整除K 代码如下: ======================================================== #include&l ...

  8. linux —— shell 编程(编程语法)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...

  9. 推荐一个markdown编辑器-MarkdownPad

    MarkdownPad - The Markdown Editor for Windows是一个很不错的windows下的markdown的编辑器,对于我这种总是记不住各种语法的人来说,非常方便. 免 ...

  10. PDOstament对象执行execute()函数,只要是sql语句正确都是返回true

    [PDO对象操作数据库] PDOstament对象执行execute()函数,只要是sql语句正确都是返回true. 问题: 想要PDO对象实现更改一条记录, 并修改是否成功要返回信息给用户. 上我的 ...