iOS开发UI篇—无限轮播(循环展示)…
2015-11-13 09:16
489 查看
文顶顶
一、简单说明
之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小技巧。
方法一:使用一个for循环,循环200次,创建200*=1000个模型,且默认程序启动后处在第100组的位置,向前有500个模型,向后也有500个模型,产生一种循环展示的假象。
代码如下:
//
// YYViewController.m
// 07-无限滚动(循环利用)
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights
reserved.
//
#import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h"
#define YYIDCell @"cell"
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView
*collectinView;
@property(nonatomic,strong)NSMutableArray *news;
@end
@implementation YYViewController
#pragma mark-懒加载
//-(NSArray *)news
//{
// if
(_news==nil) {
//
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
// }
// return
_news;
//}
-(NSMutableArray *)news
{
if
(_news==nil) {
_news=[NSMutableArray array];
for (int i=0; i<200; i++) {
NSArray *array=[YYnews
objectArrayWithFilename:@"newses.plist"];
[_news addObjectsFromArray:array];
}
}
return
_news;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
//注册cell
//
[self.collectinView registerClass:[YYimageCell class]
forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell"
bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默认处于第0组的第500个模型的左边
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:500 inSection:0]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
#pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView
{
return
1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return
self.news.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
YYcell
*cell=[collectionView
dequeueReusableCellWithReuseIdentifier:YYIDCell
forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
NSLog(@"%p,%d",cell,indexPath.item);
return
cell;
}
#pragma mark-UICollectionViewDelegate
@end
打印查看所处的索引(全程依然只创建了两个cell):
说明:
[self.collectinView scrollToItemAtIndexPath:(NSIndexPath *)
atScrollPosition:(UICollectionViewScrollPosition) animated:(BOOL)]
//默认处于第0组的第500个模型的左边
方法二:设置其有100组,那么一共有100*5=500个模型。且设置默认处于第50组的索引为0处。
代码如下:
//
// YYViewController.m
// 07-无限滚动(循环利用)
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights
reserved.
//
#import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h"
#define YYIDCell @"cell"
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView
*collectinView;
@property(nonatomic,strong)NSArray *news;
@end
@implementation YYViewController
#pragma mark-懒加载
-(NSArray *)news
{
if
(_news==nil) {
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
}
return
_news;
}
//-(NSMutableArray *)news
//{
// if
(_news==nil) {
//
_news=[NSMutableArray array];
//
for (int i=0; i<200; i++) {
//
NSArray *array=[YYnews
objectArrayWithFilename:@"newses.plist"];
//
[_news addObjectsFromArray:array];
//
}
// }
// return
_news;
//}
- (void)viewDidLoad
{
[super
viewDidLoad];
//注册cell
//
[self.collectinView registerClass:[YYimageCell class]
forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell"
bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默认处于第0组的第500个模型的左边
//
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:500 inSection:0]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:0 inSection:50]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
#pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView
{
return
100;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return
self.news.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
YYcell
*cell=[collectionView
dequeueReusableCellWithReuseIdentifier:YYIDCell
forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
NSLog(@"%p,%d",cell,indexPath.item);
return
cell;
}
#pragma mark-UICollectionViewDelegate
@end
注意:上面的两种方法都创建了大量的无用的模型,不太可取。且在实际开发中,建议模型的总数不要太大,因为在其内部需要遍历计算所有控件的frame。
如果模型数量太大,会占用资源。
改进建议:可以监听手指在上面的滚动,当停止滚动的时候,又重新设置初始的中间位置。
iOS开发UI篇—无限轮播(循环展示)
iOS开发UI篇—无限轮播(循环展示)一、简单说明
之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小技巧。
方法一:使用一个for循环,循环200次,创建200*=1000个模型,且默认程序启动后处在第100组的位置,向前有500个模型,向后也有500个模型,产生一种循环展示的假象。
代码如下:
//
// YYViewController.m
// 07-无限滚动(循环利用)
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights
reserved.
//
#import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h"
#define YYIDCell @"cell"
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView
*collectinView;
@property(nonatomic,strong)NSMutableArray *news;
@end
@implementation YYViewController
#pragma mark-懒加载
//-(NSArray *)news
//{
// if
(_news==nil) {
//
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
// }
// return
_news;
//}
-(NSMutableArray *)news
{
if
(_news==nil) {
_news=[NSMutableArray array];
for (int i=0; i<200; i++) {
NSArray *array=[YYnews
objectArrayWithFilename:@"newses.plist"];
[_news addObjectsFromArray:array];
}
}
return
_news;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
//注册cell
//
[self.collectinView registerClass:[YYimageCell class]
forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell"
bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默认处于第0组的第500个模型的左边
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:500 inSection:0]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
#pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView
{
return
1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return
self.news.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
YYcell
*cell=[collectionView
dequeueReusableCellWithReuseIdentifier:YYIDCell
forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
NSLog(@"%p,%d",cell,indexPath.item);
return
cell;
}
#pragma mark-UICollectionViewDelegate
@end
打印查看所处的索引(全程依然只创建了两个cell):
说明:
[self.collectinView scrollToItemAtIndexPath:(NSIndexPath *)
atScrollPosition:(UICollectionViewScrollPosition) animated:(BOOL)]
//默认处于第0组的第500个模型的左边
方法二:设置其有100组,那么一共有100*5=500个模型。且设置默认处于第50组的索引为0处。
代码如下:
//
// YYViewController.m
// 07-无限滚动(循环利用)
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights
reserved.
//
#import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h"
#define YYIDCell @"cell"
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView
*collectinView;
@property(nonatomic,strong)NSArray *news;
@end
@implementation YYViewController
#pragma mark-懒加载
-(NSArray *)news
{
if
(_news==nil) {
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
}
return
_news;
}
//-(NSMutableArray *)news
//{
// if
(_news==nil) {
//
_news=[NSMutableArray array];
//
for (int i=0; i<200; i++) {
//
NSArray *array=[YYnews
objectArrayWithFilename:@"newses.plist"];
//
[_news addObjectsFromArray:array];
//
}
// }
// return
_news;
//}
- (void)viewDidLoad
{
[super
viewDidLoad];
//注册cell
//
[self.collectinView registerClass:[YYimageCell class]
forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell"
bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默认处于第0组的第500个模型的左边
//
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:500 inSection:0]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath
indexPathForItem:0 inSection:50]
atScrollPosition:UICollectionViewScrollPositionLeft
animated:YES];
}
#pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView
{
return
100;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return
self.news.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
{
YYcell
*cell=[collectionView
dequeueReusableCellWithReuseIdentifier:YYIDCell
forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
NSLog(@"%p,%d",cell,indexPath.item);
return
cell;
}
#pragma mark-UICollectionViewDelegate
@end
注意:上面的两种方法都创建了大量的无用的模型,不太可取。且在实际开发中,建议模型的总数不要太大,因为在其内部需要遍历计算所有控件的frame。
如果模型数量太大,会占用资源。
改进建议:可以监听手指在上面的滚动,当停止滚动的时候,又重新设置初始的中间位置。
相关文章推荐
- iOS开发UI篇—无限轮播(功能完善)…
- 使用UITextView实现placeholder功能(XIB实现)
- 并发编程之Operation&nbsp;Queue和GCD
- IOS&nbsp;Monkey测试UI&nbsp;AutoMonkey
- 关于NGUI中一些控件的说明
- EASYUI DATAGRID 改变行值
- LeetCode Distinct Subsequences
- 【黑马程序员】String.concat(),StringBuilder和“+”的区别
- ui-router学习笔记
- Number Sequence
- EasyUI-DataGrid之批量删除
- request.getParameter如何获取radio的属性值
- Hue功能总结
- UITableView缓存池 重用
- Mathmatical Prove of the Effectiveness of Quick Sort
- BlockingQueue队列详解
- [leetcode-304]Range Sum Query 2D - Immutable(java)
- OC UI控件之UILable UIBUtton UITextFiled
- jquey学习篇 第一篇
- [非凡程序员]倒计时 uiimage uiDatePicker NSDate