iOS Development UI Article-unlimited carousel (news data show)

Source: Internet
Author: User

iOS Development UI Article-unlimited carousel (news data show)

First, the realization effect

Ii. Steps of implementation

1. Pre-preparation

(1) third-party framework for importing data-to-model mjextension

(2) Adding a plist file with "News" data to the project

    

(3) Image material for import

2. Steps and Code

(1) Create a new data model

   

  The code of the model is designed as follows:   

 YYnews.h file

1 //2 //YYnews.h3 //08-Infinite Scrolling (news data show)4 //5 6 #import<Foundation/Foundation.h>7 8 @interfaceYynews:nsobject9@property (nonatomic,copy) NSString *title;Ten@property (nonatomic,copy) NSString *icon; One @end

(2) Create a new class that inherits from Uicollectionviewcell to customize the cell.

  

(3) Create a new Xib file, associate with a custom cell

The code is designed as follows:

YYcell.h file

1 //2 //YYcell.h3 //08-Infinite Scrolling (news data show)4 //5 6 #import<UIKit/UIKit.h>7 8 @classyynews;9 @interfaceYycell:uicollectionviewcellTen@property (nonatomic,strong) yynews *News; One @end

YYCELL.M file

1 //2 //YYCELL.M3 //08-Infinite Scrolling (news data show)4 //5 6 #import "YYcell.h"7 #import "YYnews.h"8 9 @interfaceYycell ()Ten@property (Weak, nonatomic) Iboutlet UILabel *label; One@property (Weak, nonatomic) Iboutlet Uiimageview *ImageView; A  - @end - @implementationYycell the  --(void) Setnews: (Yynews *) News - { -_news=News; +self.label.text=News.title; -Self.imageview.image=[UIImage ImageNamed:news.icon]; + } A  at @end

(4) Code handling in the host controller

YYVIEWCONTROLLER.M file

1 //2 //YYVIEWCONTROLLER.M3 //  4 //5 //Created by Apple on 14-8-3.6 //Copyright (c) 2014 Yangyong. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import "MJExtension.h" One #import "YYnews.h" A #import "YYcell.h" -  - #defineYyidcell @ "Cell" the  - @interfaceYyviewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate> -@property (Weak, nonatomic) Iboutlet Uicollectionview *Collectinview; -@property (nonatomic,strong) Nsarray *News; + @end -  + @implementationYyviewcontroller A  at #pragmamark-Lazy Loading --(Nsarray *) News - { -     if(_news==Nil) { -_news=[yynews Objectarraywithfilename:@"newses.plist"]; -     } in     return_news; - } to- (void) Viewdidload + { - [Super Viewdidload]; the     //Register Cell * //[Self.collectinview Registerclass:[yyimagecell class] Forcellwithreuseidentifier:yycell]; $[Self.collectinview registernib:[uinib Nibwithnibname:@"Yycell"Bundle:nil] Forcellwithreuseidentifier:yyidcell];Panax Notoginseng      - } the  + #pragmaMark-uicollectionviewdatasource A //Total number of groups, default to 1 groups the-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView + { -     return 1; $ } $-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section - { -     returnSelf.news.count; the } - Wuyi-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath the { -Yycell *cell=[CollectionView Dequeuereusablecellwithreuseidentifier:yyidcell Forindexpath:indexpath]; WuCell.news=Self.news[indexpath.item]; -     returncell; About } $  - #pragmaMark-uicollectionviewdelegate - @end

3. Additional Information

(1) If the Collectioncell is customized in the Xib way, then it is necessary to use another method when registering the cell. [Self.collectinview Registerclass:[yyimagecell class] Forcellwithreuseidentifier:yycell]; [Self.collectinview registernib:[uinib nibwithnibname:@ "Yycell" Bundle:nil] forcellwithreuseidentifier:yyidcell]; (2) When customizing the Xib, use Collectionviewcell.  and set its identity as cell. (3) Print to view cell usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.