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.h 3// 08-Unlimited scrolling (press data show) 4//5 6 #import <Foundation/Foundation.h> 7 8 @interface YY News:nsobject 9 @property (nonatomic,copy) nsstring *title;10 @property (nonatomic,copy) nsstring *icon;11 @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.h 3// 08-Unlimited scrolling (press data Display) 4//5 6 #import <UIKit/UIKit.h> 7 8 @class yynews; 9 @inter Face Yycell:uicollectionviewcell10 @property (nonatomic,strong) yynews *news;11 @end
YYCELL.M file
1//2// YYCELL.M 3// 08-Unlimited scrolling (press data show) 4//5 6 #import "YYcell.h" 7 #import "YYnews.h" 8 9 @interface YY Cell () @property (weak, nonatomic) Iboutlet UILabel *label;11 @property (weak, nonatomic) Iboutlet Uiimageview *imagevi ew;12 @end14 @implementation YYcell15-(void) Setnews: (Yynews *) news17 { _news=news;19 self.label.text= news.title;20 self.imageview.image=[uiimage imagenamed:news.icon];21}22 @end
(4) Code handling in the host controller
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M 3//4//5//Created by Apple on 14-8-3. 6//Copyright (c) 2014 Yangyong. All rights reserved. 7//8 9 #import "YYViewController.h" #import "MJExtension.h" #import "YYnews.h" #import "YYcell.h" #define YY Idcell @ "Cell" @interface Yyviewcontroller () <uicollectionviewdatasource,uicollectionviewdelegate>17 @ Property (weak, nonatomic) Iboutlet uicollectionview *collectinview;18 @property (nonatomic,strong) Nsarray *news;19 @ END20 @implementation YYViewController22 #pragma mark-lazy load-(Nsarray *) NEWS25 {+ if (_news==nil) {27 _ News=[yynews objectarraywithfilename:@ "Newses.plist"];28}29 return _news;30}31-(void) viewDidLoad32 {[Su] Per viewdidload];34//register CELL35//[Self.collectinview Registerclass:[yyimagecell class] Forcellwithreuseidentifier: yycell];36 [Self.collectinview registernib:[uinib nibwithnibname:@ "Yycell" Bundle:nil] ForCellWithReuseIdentifier: yyidcell];37}39 #praGMA MARK-UICOLLECTIONVIEWDATASOURCE41//Total number of groups, the default is 1 group-(Nsinteger) Numberofsectionsincollectionview: ( Uicollectionview *) collectionView43 {1;45}46-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView Numberofitemsinsection: (Nsinteger) section47 {self.news.count;49}50-(Uicollectionviewcell *) Collectionv Iew: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath *) indexPath52 {$ Yycell *cell=[ CollectionView Dequeuereusablecellwithreuseidentifier:yyidcell forindexpath:indexpath];54 cell.news=self.news[ indexpath.item];55 return cell;56}57 #pragma mark-uicollectionviewdelegate59 @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
iOS Development UI Article-unlimited carousel (news data show)