国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中無限循環(huán)展示的方法

2020-02-19 15:54:00
字體:
供稿:網(wǎng)友

制作頁面的時(shí)候我們都會(huì)看到有些頁面非常好看,有一個(gè)是輪播的這個(gè)頁面,但是這個(gè)功能要使用在IOS中要怎么操作呢?它的效果怎樣呢?下面是武林技術(shù)頻道小編為大家?guī)淼膇OS中無限循環(huán)展示的方法,一起來看看吧!

無限輪播(新聞數(shù)據(jù)展示)
一、實(shí)現(xiàn)效果

2015123093114272.png (315×495)2015123093142412.png (313×493)

二、實(shí)現(xiàn)步驟

1.前期準(zhǔn)備

  (1)導(dǎo)入數(shù)據(jù)轉(zhuǎn)模型的第三方框架MJExtension

  (2)向項(xiàng)目中添加保存有“新聞”數(shù)據(jù)的plist文件

2015123093205165.png (638×170)

(3)導(dǎo)入用到的圖片素材

2.步驟和代碼

(1)新建一個(gè)數(shù)據(jù)模型

2015123093223390.png (523×130)

該模型的代碼設(shè)計(jì)如下:    

  YYnews.h文件


//
//? YYnews.h
//? 08-無限滾動(dòng)(新聞數(shù)據(jù)展示)
//

?

#import

@interface YYnews : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *icon;
@end


(2)新建一個(gè)繼承自UICollectionViewCell的類,用于自定義cell。

?

?

?

2015123093240113.png (504×139)

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

2015123093257536.png (408×86)

代碼設(shè)計(jì)如下:

   YYcell.h文件


//
//? YYcell.h
//? 08-無限滾動(dòng)(新聞數(shù)據(jù)展示)
//

?

#import

@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 ()
@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];
??? //注冊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的方式自定義的,那么在注冊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.

?

?

?

2015123093336784.png (1136×526)

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

2015123093358096.png (581×131)

三、無限輪播(循環(huán)展示)
1.簡單說明

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

2015123093417801.png (316×492)

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 ()
@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??????????? 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];
???
??? //默認(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):

2015123093439235.png (630×171)

說明:

?

?

  [self.collectinView scrollToItemAtIndexPath: atScrollPosition: animated:]

 //默認(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 ()
@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//??????????? 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];
???
??? //默認(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

以上就是iOS中無限循環(huán)展示的方法,更多內(nèi)容請繼續(xù)關(guān)注武林技術(shù)頻道的其它相關(guān)文章!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 舞钢市| 阆中市| 梁山县| 任丘市| 高雄县| 廉江市| 醴陵市| 灯塔市| 德清县| 蒲江县| 巩留县| 南投县| 南木林县| 台州市| 肇东市| 章丘市| 庆安县| 前郭尔| 平泉县| 昆明市| 阜平县| 师宗县| 柯坪县| 吕梁市| 东乡县| 从江县| 嘉义县| 毕节市| 永德县| 宜州市| 扎囊县| 电白县| 北票市| 中西区| 徐州市| 囊谦县| 交口县| 东台市| 灵璧县| 鹿邑县| 桑植县|