欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)中實現(xiàn)新聞圖片的無限循環(huán)展示的方法

 更新時間:2015年12月30日 09:37:11   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中實現(xiàn)新聞圖片的無限循環(huán)展示的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

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

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

二、實現(xiàn)步驟

1.前期準(zhǔn)備

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

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

2015123093205165.png (638×170)

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

2.步驟和代碼

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

2015123093223390.png (523×130)

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

  YYnews.h文件

復(fù)制代碼 代碼如下:

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

#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做關(guān)聯(lián)

2015123093257536.png (408×86)

代碼設(shè)計如下:

   YYcell.h文件

復(fù)制代碼 代碼如下:

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

#import <UIKit/UIKit.h>

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


 YYcell.m文件
復(fù)制代碼 代碼如下:

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

#import "YYcell.h"
#import "YYnews.h"

@interface YYcell ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


復(fù)制代碼 代碼如下:

@implementation YYcell

-(void)setNews:(YYnews *)news
{
    _news=news;
    self.label.text=news.title;
    self.imageView.image=[UIImage imageNamed:news.icon];
}

@end


(4)在主控制器中的代碼處理

  YYViewController.m文件

復(fù)制代碼 代碼如下:

//
//  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


復(fù)制代碼 代碼如下:

@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.補充說明

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

復(fù)制代碼 代碼如下:

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

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

2015123093336784.png (1136×526)

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

2015123093358096.png (581×131)

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

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

2015123093417801.png (316×492)

2.方法一:使用一個for循環(huán),循環(huán)200次,創(chuàng)建200*=1000個模型,且默認(rèn)程序啟動后處在第100組的位置,向前有500個模型,向后也有500個模型,產(chǎn)生一種循環(huán)展示的假象。

  代碼如下:

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  07-無限滾動(循環(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


復(fù)制代碼 代碼如下:

@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];
   
    //默認(rèn)處于第0組的第500個模型的左邊
    [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)建了兩個cell):

2015123093439235.png (630×171)

說明:

復(fù)制代碼 代碼如下:

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

 //默認(rèn)處于第0組的第500個模型的左邊

3.方法二:設(shè)置其有100組,那么一共有100*5=500個模型。且設(shè)置默認(rèn)處于第50組的索引為0處。

  代碼如下:

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  07-無限滾動(循環(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


復(fù)制代碼 代碼如下:

@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];
   
    //默認(rèn)處于第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
//一共多少組,默認(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)建了大量的無用的模型,不太可取。且在實際開發(fā)中,建議模型的總數(shù)不要太大,因為在其內(nèi)部需要遍歷計算所有控件的frame。

  如果模型數(shù)量太大,會占用資源。

改進(jìn)建議:可以監(jiān)聽手指在上面的滾動,當(dāng)停止?jié)L動的時候,又重新設(shè)置初始的中間位置。

相關(guān)文章

  • iOS中UIRefreshControl的基本使用詳解

    iOS中UIRefreshControl的基本使用詳解

    最近在應(yīng)用中用到UIRefreshControl,覺著有必要給大家總結(jié)介紹一下這個控件,所以下面這篇文章主要給大家介紹了關(guān)于iOS中UIRefreshControl的基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-04-04
  • IOS圖片無限輪播器的實現(xiàn)原理

    IOS圖片無限輪播器的實現(xiàn)原理

    這篇文章主要介紹了IOS圖片無限輪播器的實現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • iOS 中Swift仿微信添加提示小紅點功能(無數(shù)字)

    iOS 中Swift仿微信添加提示小紅點功能(無數(shù)字)

    這篇文章主要介紹了iOS 中Swift仿微信添加提示小紅點功能(無數(shù)字),非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • IOS手勢操作(拖動、捏合、旋轉(zhuǎn)、點按、長按、輕掃、自定義)

    IOS手勢操作(拖動、捏合、旋轉(zhuǎn)、點按、長按、輕掃、自定義)

    這篇文章主要介紹了IOS手勢操作(拖動、捏合、旋轉(zhuǎn)、點按、長按、輕掃、自定義),需要的朋友可以參考下
    2015-07-07
  • iOS用AutoLayout實現(xiàn)分頁滾動功能

    iOS用AutoLayout實現(xiàn)分頁滾動功能

    這篇文章主要給大家介紹了關(guān)于iOS用AutoLayout實現(xiàn)分頁滾動功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • iOS中定位(location manager )出現(xiàn)log日志的解決辦法

    iOS中定位(location manager )出現(xiàn)log日志的解決辦法

    這篇文章主要給大家介紹了關(guān)于iOS中定位(location manager )出現(xiàn)log日志的解決辦法,文中通過示例代碼將解決的辦法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • iOS藍(lán)牙開發(fā) 藍(lán)牙連接和數(shù)據(jù)讀寫

    iOS藍(lán)牙開發(fā) 藍(lán)牙連接和數(shù)據(jù)讀寫

    這篇文章主要為大家詳細(xì)介紹了iOS藍(lán)牙開發(fā)之藍(lán)牙連接和數(shù)據(jù)讀寫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS給密碼進(jìn)行加密的方法

    iOS給密碼進(jìn)行加密的方法

    這篇文章主要介紹了iOS給密碼進(jìn)行加密的方法,需要的朋友可以參考下
    2017-06-06
  • 解析iOS10中的極光推送消息的適配

    解析iOS10中的極光推送消息的適配

    這篇文章主要介紹了解析iOS10中的極光推送消息的適配的相關(guān)資料,我們需要先安裝Xcode8.0版本,接下來本文分步驟詳細(xì)給大家介紹,需要的朋友可以參考下
    2016-09-09
  • iOS實現(xiàn)步驟進(jìn)度條功能實例代碼

    iOS實現(xiàn)步驟進(jìn)度條功能實例代碼

    這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)步驟進(jìn)度條功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評論