服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现列表与网格两种视图的相互切换

iOS实现列表与网格两种视图的相互切换

2021-02-04 15:45iOS开发网 IOS

相信大家应该也都发现了,在现在很多的电商app中,都会有列表视图和网格两种视图的相互切换。例如京东和淘宝。这样更利于提高用户的体验度,所以这篇文章小编就是大家分享下利用iOS实现列表与网格两种视图相互切换的方法

下图为京东商城的截图

iOS实现列表与网格两种视图的相互切换

iOS实现列表与网格两种视图的相互切换

很多人看到这个,第一眼想到的是用tableviewcollectionview来做切换,笔者刚开始也是认为这么做,后来发现还有一个非常的简单方法,就可以实现这个功能。

实现代码

1、首先创建一个collectionview。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (uicollectionview *)collectionview
{
  if (!_collectionview)
  {
    uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc] init];
    //设置滚动方向
    [flowlayout setscrolldirection:uicollectionviewscrolldirectionvertical];
    //左右间距
    flowlayout.minimuminteritemspacing = 2;
    //上下间距
    flowlayout.minimumlinespacing = 2;
    _collectionview = [[uicollectionview alloc] initwithframe:cgrectmake(2 , 2 , self.view.bounds.size.width - 4, self.view.bounds.size.height - 4) collectionviewlayout:flowlayout];
    _collectionview.delegate = self;
    _collectionview.datasource = self;
    _collectionview.showsverticalscrollindicator = no;
    _collectionview.showshorizontalscrollindicator = no;
    [_collectionview setbackgroundcolor:[uicolor clearcolor]];
    //注册cell
    [_collectionview registerclass:[gridlistcollectionviewcell class] forcellwithreuseidentifier:kcellidentifier_collectionviewcell];
  }
  return _collectionview;
}

然后去京东商城抓取json数据,再去解析数据装入模型,objectwithdictionary:是将字典转化为模型,这个工具是我用 runtime 写的,一行代码解析数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)viewdidload
{
  [super viewdidload];
  // do any additional setup after loading the view, typically from a nib.
 
  // 默认列表视图
  _isgrid = no;
 
  nsstring *path = [[nsbundle mainbundle] pathforresource:@"product" oftype:@"json"];
  nsdata *data = [nsdata datawithcontentsoffile:path];
  nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil];
 
  [self.view addsubview:self.collectionview];
 
  nsarray *products = dict[@"wareinfo"];
  for (id obj in products) {
    [self.datasource addobject:[gridlistmodel objectwithdictionary:obj]];
  }
}

再去自定义collectionviewcell,给cell添加一个属性isgrid,用来判断是列表还是格子视图。

.h文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <uikit/uikit.h>
 
#define kcellidentifier_collectionviewcell @"gridlistcollectionviewcell"
 
@class gridlistmodel;
 
@interface gridlistcollectionviewcell : uicollectionviewcell
 
/**
 0:列表视图,1:格子视图
 */
@property (nonatomic, assign) bool isgrid;
 
@property (nonatomic, strong) gridlistmodel *model;
 
@end

.m文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#import "gridlistcollectionviewcell.h"
#import "gridlistmodel.h"
#import "uiimageview+webcache.h"
 
#define screenwidth ([uiscreen mainscreen].bounds.size.width)
 
@interface gridlistcollectionviewcell ()
 
@property (nonatomic, strong) uiimageview *imagev;
@property (nonatomic, strong) uilabel *titlelabel;
@property (nonatomic, strong) uilabel *pricelabel;
 
@end
 
@implementation gridlistcollectionviewcell
 
- (instancetype)initwithframe:(cgrect)frame
{
  self = [super initwithframe:frame];
  if (self) {
    [self configureui];
  }
  return self;
}
 
- (void)configureui
{
  _imagev = [[uiimageview alloc] initwithframe:cgrectzero];
  [self.contentview addsubview:_imagev];
 
  _titlelabel = [[uilabel alloc] initwithframe:cgrectzero];
  _titlelabel.numberoflines = 0;
  _titlelabel.font = [uifont boldsystemfontofsize:14];
  [self.contentview addsubview:_titlelabel];
 
  _pricelabel = [[uilabel alloc] initwithframe:cgrectzero];
  _pricelabel.textcolor = [uicolor redcolor];
  _pricelabel.font = [uifont systemfontofsize:16];
  [self.contentview addsubview:_pricelabel];
}
 
- (void)setisgrid:(bool)isgrid
{
  _isgrid = isgrid;
 
  if (isgrid) {
    _imagev.frame = cgrectmake(5, 5, self.bounds.size.width - 60, self.bounds.size.width - 60);
    _titlelabel.frame = cgrectmake(5, self.bounds.size.width - 45, screenwidth/2, 20);
    _pricelabel.frame = cgrectmake(5, self.bounds.size.width - 20, screenwidth/2, 20);
  } else {
    _imagev.frame = cgrectmake(5, 5, self.bounds.size.height - 10, self.bounds.size.height - 10);
    _titlelabel.frame = cgrectmake(self.bounds.size.height + 10, 0, screenwidth/2, self.bounds.size.height - 20);;
    _pricelabel.frame = cgrectmake(self.bounds.size.height + 10, self.bounds.size.height - 30, screenwidth/2, 20);;
  }
}
 
- (void)setmodel:(gridlistmodel *)model
{
  _model = model;
 
  [_imagev sd_setimagewithurl:[nsurl urlwithstring:model.imageurl]];
  _titlelabel.text = model.wname;
  _pricelabel.text = [nsstring stringwithformat:@"¥%.2f",model.jdprice];
}
 
@end

再添加一个切换视图的按钮,按钮的点击事件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma mark - action
 
- (ibaction)onbtnclick:(id)sender
{
  _isgrid = !_isgrid;
  [self.collectionview reloaddata];
 
  if (_isgrid) {
    [self.swithbtn setimage:[uiimage imagenamed:@"product_list_grid_btn"] forstate:0];
  } else {
    [self.swithbtn setimage:[uiimage imagenamed:@"product_list_list_btn"] forstate:0];
  }
}

最后还要设置一下切换时的collectionviewitemsize

?
1
2
3
4
5
6
7
8
- (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath
{
  if (_isgrid) {
    return cgsizemake((screenwidth - 6) / 2, (screenwidth - 6) / 2 + 40);
  } else {
    return cgsizemake(screenwidth - 4, (screenwidth - 6) / 4 + 20);
  }
}

这样子就大体实现了列表视图和网格视图的相互切换,是不是很简单。

iOS实现列表与网格两种视图的相互切换

总结

以上就是这篇文章的全部内容了,可能由于笔者水平有限,文中如果有错误的地方,还望大家能够指出。或者有更好的方法和建议,我们也可以一起交流。希望这篇文章的内容对大家能有所帮助。

延伸 · 阅读

精彩推荐
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19