iOS開發(fā)中實(shí)現(xiàn)新聞圖片的無限循環(huán)展示的方法
無限輪播(新聞數(shù)據(jù)展示)
一、實(shí)現(xiàn)效果


二、實(shí)現(xiàn)步驟
1.前期準(zhǔn)備
?。?)導(dǎo)入數(shù)據(jù)轉(zhuǎn)模型的第三方框架MJExtension
(2)向項(xiàng)目中添加保存有“新聞”數(shù)據(jù)的plist文件

(3)導(dǎo)入用到的圖片素材
2.步驟和代碼
(1)新建一個(gè)數(shù)據(jù)模型

該模型的代碼設(shè)計(jì)如下:
YYnews.h文件
//
// YYnews.h
// 08-無限滾動(dòng)(新聞數(shù)據(jù)展示)
//
#import <Foundation/Foundation.h>
@interface YYnews : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *icon;
@end
(2)新建一個(gè)繼承自UICollectionViewCell的類,用于自定義cell。

(3)新建一個(gè)xib文件,和自定義的cell做關(guān)聯(lián)

代碼設(shè)計(jì)如下:
YYcell.h文件
//
// YYcell.h
// 08-無限滾動(dòng)(新聞數(shù)據(jù)展示)
//
#import <UIKit/UIKit.h>
@class YYnews;
@interface YYcell : UICollectionViewCell
@property(nonatomic,strong)YYnews *news;
@end
YYcell.m文件
//
// YYcell.m
// 08-無限滾動(dòng)(新聞數(shù)據(jù)展示)
//
#import "YYcell.h"
#import "YYnews.h"
@interface YYcell ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation YYcell
-(void)setNews:(YYnews *)news
{
_news=news;
self.label.text=news.title;
self.imageView.image=[UIImage imageNamed:news.icon];
}
@end
(4)在主控制器中的代碼處理
YYViewController.m文件
//
// YYViewController.m
//
//
// 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 ()<UICollectionViewDataSource,UICollectionViewDelegate>
@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;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//注冊(cè)cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
}
#pragma mark- UICollectionViewDataSource
//一共多少組,默認(rèn)為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];
return cell;
}
#pragma mark-UICollectionViewDelegate
@end
3.補(bǔ)充說明
(1)如果collectionCell是以xib的方式自定義的,那么在注冊(cè)cell的時(shí)候,需要使用另外一種方式。
[self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
(2)在自定義xib的時(shí)候,使用collectionViewCell。并設(shè)置其標(biāo)識(shí)為cell.

(3)打印查看cell的利用情況

三、無限輪播(循環(huán)展示)
1.簡(jiǎn)單說明
之前的程序還存在一個(gè)問題,那就是不能循環(huán)展示,因?yàn)閜list文件中只有五個(gè)數(shù)組,因此第一個(gè)和最后一個(gè)之后就沒有了,下面介紹處理這種循環(huán)展示問題的小技巧。

2.方法一:使用一個(gè)for循環(huán),循環(huán)200次,創(chuàng)建200*=1000個(gè)模型,且默認(rèn)程序啟動(dòng)后處在第100組的位置,向前有500個(gè)模型,向后也有500個(gè)模型,產(chǎn)生一種循環(huán)展示的假象。
代碼如下:
//
// YYViewController.m
// 07-無限滾動(dòng)(循環(huán)利用)
//
// 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 ()<UICollectionViewDataSource,UICollectionViewDelegate>
@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];
//注冊(cè)cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默認(rèn)處于第0組的第500個(gè)模型的左邊
[self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark- UICollectionViewDataSource
//一共多少組,默認(rèn)為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
打印查看所處的索引(全程依然只創(chuàng)建了兩個(gè)cell):

說明:
[self.collectinView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>]
//默認(rèn)處于第0組的第500個(gè)模型的左邊
3.方法二:設(shè)置其有100組,那么一共有100*5=500個(gè)模型。且設(shè)置默認(rèn)處于第50組的索引為0處。
代碼如下:
//
// YYViewController.m
// 07-無限滾動(dòng)(循環(huán)利用)
//
// 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 ()<UICollectionViewDataSource,UICollectionViewDelegate>
@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];
//注冊(cè)cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
//默認(rèn)處于第0組的第500個(gè)模型的左邊
// [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
//一共多少組,默認(rèn)為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
注意:上面的兩種方法都創(chuàng)建了大量的無用的模型,不太可取。且在實(shí)際開發(fā)中,建議模型的總數(shù)不要太大,因?yàn)樵谄鋬?nèi)部需要遍歷計(jì)算所有控件的frame。
如果模型數(shù)量太大,會(huì)占用資源。
改進(jìn)建議:可以監(jiān)聽手指在上面的滾動(dòng),當(dāng)停止?jié)L動(dòng)的時(shí)候,又重新設(shè)置初始的中間位置。
相關(guān)文章
iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
ios開發(fā):一個(gè)音樂播放器的設(shè)計(jì)與實(shí)現(xiàn)案例
本篇文章主要介紹了ios開發(fā):一個(gè)音樂播放器的設(shè)計(jì)與實(shí)現(xiàn)案例,具有一定的參考價(jià)值,有需要的小伙伴可以參考下。2016-11-11
iOS動(dòng)畫實(shí)現(xiàn)雨花與櫻花特效
小編今天為大家?guī)硪粓?chǎng)淅淅瀝瀝的夜空之雨和滿天飛舞的櫻花之戀,希望能在炎炎夏日為您帶來一絲清爽的涼意!學(xué)習(xí)iOS動(dòng)畫的小伙伴們可以參考學(xué)習(xí)。2016-08-08
iOS實(shí)現(xiàn)相冊(cè)和網(wǎng)絡(luò)圖片的存取
本篇文章主要介紹了iOS實(shí)現(xiàn)相冊(cè)和網(wǎng)絡(luò)圖片的存取,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
iOS仿支付寶芝麻信用分?jǐn)?shù)儀表盤動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了iOS仿支付寶芝麻信用分?jǐn)?shù)儀表盤動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS App開發(fā)中修改UILabel默認(rèn)字體的方法
UILabel是控制字體顯示的主要方式,這里我們就來看看通過NSAttributedText和NSMutableAttributedText這兩個(gè)類或者用runtime的方式來在iOS App開發(fā)中修改UILabel默認(rèn)字體的方法2016-07-07
iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例
這篇文章主要介紹了iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例,幫助大家更好的進(jìn)行ios開發(fā),感興趣的朋友可以了解下2020-10-10

