概述


  • 实现效果

  • 设计思路

    • 采用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. SQL Server 中各个系统表的作用

    来源:http://www.hoky.org/blog/ sysaltfiles 主数据库 保存数据库的文件syscharsets 主数据库 字符集与排序顺序sysconfigures 主数据库 配置 ...

  2. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. poj 2425 A Chess Game(SG函数)

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3551   Accepted: 1440 Desc ...

  4. Debian添加软件源

    安装完渗透测试系统kali linux后,默认的只有security这个源,只更新那些集成的安全软件,不能安装其他新软件,官网给出了3类源: Kali Linux提供了3类软件源,这些源在世界各地都有 ...

  5. 2013=7=12 ACM培训第一天

    ACM培训第一天,尽管我嘴上说是来打酱油的,但我非常想学好.1.一定要多思考,多总结:2.多问同学 :3.学会向女生说话,大胆,自信.(今天有女生向我说话了,很高兴.她很大胆,我要向她学习...... ...

  6. [经典] Best Time to Buy and Sell Stock

    这一系列求最优值的问题变种挺多 1. Say you have an array for which the ith element is the price of a given stock on ...

  7. springMVC能做什么,做j2ee时候要考虑什么

    转载: http://jinnianshilongnian.iteye.com/category/231099 [置顶] 跟我学SpringMVC目录汇总贴.PDF下载.源码下载 博客分类: 跟开涛学 ...

  8. Java 集合框架 ArrayList 源码剖析

    总体介绍 ArrayList实现了List接口,是顺序容器,即元素存放的数据与放进去的顺序相同,允许放入null元素,底层通过数组实现.除该类未实现同步外,其余跟Vector大致相同.每个ArrayL ...

  9. Java做界面的感想。。

    我用Swing做出的例子: JavaFX做出的界面: 后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下 ...

  10. 与IO相关的等待事件troubleshooting-系列5

    'db file scattered read'         这是另一种常见的等待事件.他产生于Oracle从磁盘读取多个块到Buffer Cache中非连续(" scattered&q ...