概述


  • 实现效果

  • 设计思路

    • 采用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. Android 逐帧动画isRunning 一直返回true的问题

    AnimationDrawabl主要通过xml实现逐帧动画,SDK实例如下: An AnimationDrawable defined in XML consists of a single < ...

  2. 【转】SendMessage及WPRAME、LPARAME

    原文网址:http://www.cnblogs.com/renyuan/archive/2012/11/26/2789103.html SendMessage及WPRAME.LPARAME typed ...

  3. 局域网内通过UDP协议进行传输接受数据——AsyncUdpSocket

    在相同的局域网内,可以通过Udp协议进行数据的传输和接收,Udp协议与Http协议不同,Udp更加方便快捷,省去了很多步骤,但是也有很多传输问题,在局域网内小范围传输数据时Udp还是非常能够胜任的. ...

  4. 【Javascript&Jquery基础归纳】- 加载相关

    1.window.onload 必须等到Dom所有元素.包括图片加载完毕后加载,只能编写一个. 2.$(document).ready()      DOM结构加载完毕后马上执行,并且可以编写多个. ...

  5. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. Tomcat绑定多个IP地址 多域名绑定

    http://blog.csdn.net/stevenyanzhi/article/details/6029776 Tomcat绑定多个IP地址 如果一台服务机上有多个IP地址又有多个工程如何一个IP ...

  7. 用网页server实现钢琴弹奏(使用Wizwiki-W7500)

      我将为大家介绍一个能够弹钢琴的网页server. 你仅仅须要打开网页,跟着以下步骤,用这个WIZwiki-W7500项目,就能轻松实现用网页server弹钢琴. 很easy. 我们開始吧! 第 ...

  8. Delphi Memo的记事本功能

    Delphi Memo的记事本功能           下载地址 : http://download.csdn.net/detail/teststudio/6412883 这个代码实现了Windows ...

  9. gitlab hooks

    之前一直用文件同步工具进行两台服务器之间的同步 最初的目的是吧gitlab的代码库同步到开发环境中运行 实时的 ,后来由于gitlab的代码仓库的代码不是实时的 于是在即使同步不适合 我的需求. 后来 ...

  10. apache不解析php文档?提示需要下载(转)

    在httpd.cong中 LoadModule php5_module modules/libphp5.so #这一行php5安装的时候就已经自动添加上了 AddType application/x- ...