当前位置: 首页 > news >正文

无限轮播(循环展示)

无限轮播(循环展示)

一、简单说明

  之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小技巧。

  

方法一:使用一个for循环,循环200次,创建200*=1000个模型,且默认程序启动后处在第100组的位置,向前有500个模型,向后也有500个模型,产生一种循环展示的假象。

  代码如下:

复制代码
 8 
 9 #import "YYViewController.h"
10 #import "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13 
14 #define YYIDCell @"cell"
15 
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSMutableArray *news;
19 @end
20 
21 @implementation YYViewController
22 
23 #pragma mark-懒加载
24 //-(NSArray *)news
25 //{
26 //    if (_news==nil) {
27 //        _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28 //    }
29 //    return _news;
30 //}
31 -(NSMutableArray *)news
32 {
33     if (_news==nil) {
34         _news=[NSMutableArray array];
35         for (int i=0; i<200; i++) {
36             NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37             [_news addObjectsFromArray:array];
38         }
39     }
40     return _news;
41 }
42 
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注册cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49     
50     //默认处于第0组的第500个模型的左边
51     [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52     
53 }
54 
55 #pragma mark- UICollectionViewDataSource
56 //一共多少组,默认为1组
57 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
58 {
59     return 1;
60 }
61 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
62 {
63     return self.news.count;
64 }
65 
66 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
67 {
68     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
69     cell.news=self.news[indexPath.item];
70     NSLog(@"%p,%d",cell,indexPath.item);
71     return cell;
72 }
73 
74 #pragma mark-UICollectionViewDelegate
75 @end
复制代码

  打印查看所处的索引(全程依然只创建了两个cell):

  

说明:

  [self.collectinView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>]

 //默认处于第0组的第500个模型的左边

方法二:设置其有100组,那么一共有100*5=500个模型。且设置默认处于第50组的索引为0处。

  代码如下:

复制代码
 8 
 9 #import "YYViewController.h"
10 #import "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13 
14 #define YYIDCell @"cell"
15 
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSArray *news;
19 @end
20 
21 @implementation YYViewController
22 
23 #pragma mark-懒加载
24 -(NSArray *)news
25 {
26     if (_news==nil) {
27         _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28     }
29     return _news;
30 }
31 //-(NSMutableArray *)news
32 //{
33 //    if (_news==nil) {
34 //        _news=[NSMutableArray array];
35 //        for (int i=0; i<200; i++) {
36 //            NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37 //            [_news addObjectsFromArray:array];
38 //        }
39 //    }
40 //    return _news;
41 //}
42 
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注册cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49     
50     //默认处于第0组的第500个模型的左边
51 //    [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52     
53      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
54     
55 }
56 
57 #pragma mark- UICollectionViewDataSource
58 //一共多少组,默认为1组
59 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
60 {
61     return 100;
62 }
63 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
64 {
65     return self.news.count;
66 }
67 
68 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
69 {
70     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
71     cell.news=self.news[indexPath.item];
72     NSLog(@"%p,%d",cell,indexPath.item);
73     return cell;
74 }
75 
76 #pragma mark-UICollectionViewDelegate
77 @end
复制代码

注意:上面的两种方法都创建了大量的无用的模型,不太可取。且在实际开发中,建议模型的总数不要太大,因为在其内部需要遍历计算所有控件的frame。

  如果模型数量太大,会占用资源。

改进建议:可以监听手指在上面的滚动,当停止滚动的时候,又重新设置初始的中间位置。

转载于:https://www.cnblogs.com/crash-wu/p/4797364.html

相关文章:

  • ArcGIS API for JavaScript开发笔记(一)——ArcGIS for Javascript API 3.14本地部署
  • Vs2017获取Git空仓库后创建解决方案及项目无法推送,推送失败的问题.
  • 【编程程序猿艺术】学习记录1:指针向左翻转法的旋转串
  • Windows XP 死期将至 微软终于伸援手了
  • xen的实时迁移(四)
  • 递归3--棋盘分割
  • android网络开源框架volley(五岁以下儿童)——volley一些细节
  • 查看自己的电脑的内存扩充-最大
  • MySQL错误Another MySQL daemon already running with the same unix socket.v
  • CSS制作响应式正方形及其应用
  • css中attribute selector及pseudo class
  • “考虑不全面”导致的大问题!!!
  • [Linux]于Mac在配置Linuxserver安装Nginx+PHP
  • Multimodal —— 看图说话(Image Caption)任务的论文笔记(二)引入attention机制
  • NFS服务配置固定端口
  • 【刷算法】从上往下打印二叉树
  • android图片蒙层
  • C# 免费离线人脸识别 2.0 Demo
  • HTTP中GET与POST的区别 99%的错误认识
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • Netty源码解析1-Buffer
  • REST架构的思考
  • Sass 快速入门教程
  • session共享问题解决方案
  • socket.io+express实现聊天室的思考(三)
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 分布式事物理论与实践
  • 前端面试题总结
  • 微信开源mars源码分析1—上层samples分析
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 延迟脚本的方式
  • 智能合约Solidity教程-事件和日志(一)
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • (4)Elastix图像配准:3D图像
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (区间dp) (经典例题) 石子合并
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • .Net 8.0 新的变化
  • .Net Core 中间件验签
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .NET 分布式技术比较
  • .net 怎么循环得到数组里的值_关于js数组
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)
  • /etc/sudoer文件配置简析
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • [ vulhub漏洞复现篇 ] GhostScript 沙箱绕过(任意命令执行)漏洞CVE-2019-6116
  • [android] 请求码和结果码的作用
  • [BZOJ 2142]礼物(扩展Lucas定理)
  • [c#基础]DataTable的Select方法
  • [docker] Docker的私有仓库部署——Harbor
  • [Flexbox] Using order to rearrange flexbox children
  • [hdu 1711] Number Sequence [kmp]
  • [ios-必看] IOS调试技巧:当程序崩溃的时候怎么办 iphone IOS