亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS開發中實現新聞圖片的無限循環展示的方法

2020-07-26 03:30:28
字體:
來源:轉載
供稿:網友

無限輪播(新聞數據展示)
一、實現效果

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

二、實現步驟

1.前期準備

  (1)導入數據轉模型的第三方框架MJExtension

 ?。?)向項目中添加保存有“新聞”數據的plist文件

2015123093205165.png (638×170)

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

2.步驟和代碼

(1)新建一個數據模型

2015123093223390.png (523×130)

該模型的代碼設計如下:    

  YYnews.h文件

復制代碼 代碼如下:

//
//  YYnews.h
//  08-無限滾動(新聞數據展示)
//

#import <Foundation/Foundation.h>

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


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

2015123093240113.png (504×139)

(3)新建一個xib文件,和自定義的cell做關聯

2015123093257536.png (408×86)

代碼設計如下:

   YYcell.h文件

復制代碼 代碼如下:

//
//  YYcell.h
//  08-無限滾動(新聞數據展示)
//

#import <UIKit/UIKit.h>

@class YYnews;
@interface YYcell : UICollectionViewCell
@property(nonatomic,strong)YYnews *news;
@end


 YYcell.m文件
復制代碼 代碼如下:

//
//  YYcell.m
//  08-無限滾動(新聞數據展示)
//

#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];
    //注冊cell
//    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
    [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
   
}

#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];
    return cell;
}

#pragma mark-UICollectionViewDelegate
@end


3.補充說明

(1)如果collectionCell是以xib的方式自定義的,那么在注冊cell的時候,需要使用另外一種方式。

復制代碼 代碼如下:

[self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];

(2)在自定義xib的時候,使用collectionViewCell。并設置其標識為cell.

2015123093336784.png (1136×526)

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

2015123093358096.png (581×131)

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

  之前的程序還存在一個問題,那就是不能循環展示,因為plist文件中只有五個數組,因此第一個和最后一個之后就沒有了,下面介紹處理這種循環展示問題的小技巧。

2015123093417801.png (316×492)

2.方法一:使用一個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 ()<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];
    //注冊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):

2015123093439235.png (630×171)

說明:

復制代碼 代碼如下:

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

 //默認處于第0組的第500個模型的左邊

3.方法二:設置其有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 ()<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];
    //注冊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。

  如果模型數量太大,會占用資源。

改進建議:可以監聽手指在上面的滾動,當停止滾動的時候,又重新設置初始的中間位置。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久综合色影院| 粉嫩av一区二区三区免费野| 日韩精品中文字幕在线| 欧美性开放视频| 精品国产欧美成人夜夜嗨| 国产精品高潮呻吟久久av野狼| 亚洲bt天天射| 色老头一区二区三区| 亚洲美女av在线播放| 最近2019免费中文字幕视频三| 日韩av快播网址| 91网站免费观看| 国产成人精品免高潮费视频| 亚洲在线视频观看| 在线性视频日韩欧美| 亚洲男女自偷自拍图片另类| 中文字幕亚洲综合久久| 国产日韩欧美在线视频观看| 日本不卡高字幕在线2019| 欧美性猛交xxxx乱大交蜜桃| 欧美成人免费观看| 国产精品久久久久av| 都市激情亚洲色图| 亚洲美女福利视频网站| 欧美一区二区影院| 日韩激情第一页| 2018国产精品视频| 欧美中文字幕精品| 亚洲精品视频网上网址在线观看| 国产一区视频在线| 欧美另类老肥妇| 2018国产精品视频| 国产精品久久99久久| 精品福利视频导航| 久久久久久亚洲精品中文字幕| 色午夜这里只有精品| 成人妇女淫片aaaa视频| 亚洲裸体xxxx| 97视频在线观看免费高清完整版在线观看| 国产在线日韩在线| 亚洲在线www| 亚洲一区免费网站| 欧美激情xxxx性bbbb| 欧美日韩一区二区精品| 亚洲人线精品午夜| 国产视频自拍一区| 免费不卡欧美自拍视频| 黄色成人在线免费| 亚洲福利视频网| 成人免费网站在线| 26uuu另类亚洲欧美日本老年| 国产91热爆ts人妖在线| 在线丨暗呦小u女国产精品| 久久久久久综合网天天| 国产精品视频最多的网站| 亚洲成人久久电影| 日韩av在线看| 亚洲欧洲高清在线| 91福利视频网| 欧美日韩中文在线观看| 亚州精品天堂中文字幕| 亚洲另类图片色| 日韩av免费在线观看| 91禁外国网站| 午夜精品免费视频| 性欧美暴力猛交69hd| 久久久久久久999精品视频| 国产精品丝袜白浆摸在线| 国产精品视频久| 亚洲无限乱码一二三四麻| 欧美香蕉大胸在线视频观看| 欧美精品在线看| 国语自产在线不卡| 亚洲精品久久久久国产| 91精品国产综合久久男男| 亚洲精品福利资源站| 日韩美女毛茸茸| 在线日韩第一页| 亚洲系列中文字幕| 亚洲最大av在线| 亚洲无线码在线一区观看| 日韩**中文字幕毛片| 亚洲片国产一区一级在线观看| 久久久久日韩精品久久久男男| 日韩视频―中文字幕| 久久精品中文字幕一区| 92看片淫黄大片看国产片| 色先锋久久影院av| 欧美精品第一页在线播放| 中文字幕精品久久| 久久久成人精品视频| 中文字幕久热精品在线视频| 日本电影亚洲天堂| 91在线视频九色| 亚洲天堂第一页| 国产精品久久久久久网站| 国产成人av在线| 欧美猛交ⅹxxx乱大交视频| 国产亚洲一区二区在线| 国产日韩欧美中文| 欧美午夜精品久久久久久浪潮| 久久久久久97| 精品国产一区二区三区久久| 成人www视频在线观看| 国产精品福利小视频| 欧美日韩国产精品一区二区三区四区| 久久精品国亚洲| 欧美日韩国产在线播放| 久久久极品av| 亚洲视屏在线播放| 精品国产91乱高清在线观看| 色噜噜狠狠狠综合曰曰曰| 一个色综合导航| 都市激情亚洲色图| 国产美女被下药99| 欧美日韩亚洲视频| 久久精品国产2020观看福利| 日韩久久精品成人| 久久综合久中文字幕青草| 亚洲精品视频久久| 在线日韩第一页| 亚洲视频在线视频| 国模私拍一区二区三区| 国产精品久久久久999| 欧美做受高潮1| 2019国产精品自在线拍国产不卡| 国产欧美一区二区三区在线| 成人a在线视频| 欧美日韩加勒比精品一区| 欧美制服第一页| 欧美黑人国产人伦爽爽爽| 成人高h视频在线| 久久91超碰青草是什么| 草民午夜欧美限制a级福利片| 大伊人狠狠躁夜夜躁av一区| 精品毛片网大全| 亚洲国产成人爱av在线播放| 日韩av黄色在线观看| 国产91精品青草社区| 色系列之999| 九九九热精品免费视频观看网站| 欧美极品第一页| 91av视频在线观看| 欧美精品在线免费播放| 日产精品99久久久久久| 亚洲性日韩精品一区二区| 一区二区欧美亚洲| 久久综合久久八八| 精品中文字幕视频| 亚洲精品一区二区三区婷婷月| 欧美激情一区二区久久久| 国产精品极品美女粉嫩高清在线| 亚洲成年人影院在线| 国产日韩在线精品av| 国产精品偷伦免费视频观看的| 日本不卡高字幕在线2019| 精品无人国产偷自产在线| 日韩有码视频在线| 国内精品400部情侣激情| 亚洲片国产一区一级在线观看| 久久久久久网址| 成人疯狂猛交xxx| 7777kkkk成人观看| 日本韩国欧美精品大片卡二|