iOS開發UI篇—無限輪播(循環展示)
一、簡單說明
之前的程序還存在一個問題,那就是不能循環展示,因為plist文件中只有五個數組,因此第一個和最后一個之后就沒有了,下面介紹處理這種循環展示問題的小技巧。
方法一:使用一個for循環,循環200次,創建200*=1000個模型,且默認程序啟動后處在第100組的位置,向前有500個模型,向后也有500個模型,產生一種循環展示的假象。
代碼如下:
1 // 2 // YYViewController.m 3 // 07-無限滾動(循環利用) 4 // 5 // Created by apple on 14-8-3. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 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處。
代碼如下:
1 // 2 // YYViewController.m 3 // 07-無限滾動(循環利用) 4 // 5 // Created by apple on 14-8-3. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 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。
如果模型數量太大,會占用資源。
改進建議:可以監聽手指在上面的滾動,當停止滾動的時候,又重新設置初始的中間位置。