Bowencollectionviewcell.xib
1 #import<UIKit/UIKit.h>2 3 @interfaceBowencollectionviewcell:uicollectionviewcell4 5@property (Weak, nonatomic) Iboutlet Uiimageview *Iconimageview;6 7@property (Weak, nonatomic) Iboutlet UILabel *Lalname;8@property (Weak, nonatomic) Iboutlet UILabel *lblposition;9@property (Weak, nonatomic) Iboutlet UIButton *Btnmaninfo;Ten@property (Weak, nonatomic) iboutlet Uitextview *textviewstory; One A @end - - #import "BowenCollectionViewCell.h" the - @implementationBowencollectionviewcell - -- (void) awakefromnib { + //Initialization Code - } + A @end at - #import "ViewController.h" - #import "BowenCollectionViewCell.h" - - /* - Collection View: Uicollertionview in three protocols, two proxies, four or five methods - to Uicollectionviewdatasource Loading Data + uicollectionviewdelegate Execution Agent - uicollectionviewdelegateflowlayout Layout the * Use steps $ 0. Create a cell IDPanax Notoginseng 1. Create a layout (landscape layout, portrait layout) - 2. Create a collection view, set up an agent the 3. Register the cell + 4. Load the View A */ the + @interfaceViewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> - $@property (nonatomic, strong) Uicollectionview *CollectionView; $@property (nonatomic, strong) Nsindexpath *Indexpath; - - @end the - @implementationViewcontrollerWuyi the StaticNSString *cellidentyfier =@"Cellindentyfier"; - Wu- (void) Viewdidload { - [Super Viewdidload]; About $ //1. Create a layout (landscape layout, portrait layout) -Uicollectionviewflowlayout *layout =[[Uicollectionviewflowlayout alloc] init]; - [Layout setscrolldirection:uicollectionviewscrolldirectionhorizontal]; - A //2. Create a collection view, set up an agent +Self.collectionview =[[Uicollectionview alloc] initWithFrame:self.view.bounds collectionviewlayout:layout]; theSelf.collectionView.dataSource =Self ; -Self.collectionview.Delegate=Self ; $ //3. Register the cell (item) the[Self.collectionview Registerclass:[uicollectionviewcellclass] forcellwithreuseidentifier:cellidentyfier]; the //4. Load the View the [Self.view AddSubview:self.collectionView]; the //loading the nib file -[Self.collectionview registernib:[uinib Nibwithnibname:@"Bowencollectionviewcell"Bundle:[nsbundle Mainbundle]] forcellwithreuseidentifier:cellidentyfier]; in the the } About the #pragmaMark-Data source the //number of load groups the-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView + { - return 2; the Bayi } the //Number of item the-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section - { - return 2; the } the //How to load data in a cell in item the-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath the { - //the cell of the collection view is itself a view, and nothing comes with the control the //If you want to use the item of the collection view the //1. Add controls directly above (not recommended) Official Rules: This loading method can only load data the //2. Custom Item Usage94Bowencollectionviewcell *cell =[CollectionView dequeuereusablecellwithreuseidentifier:cellidentyfier Forindexpath:indexpath]; theCell.contentView.backgroundColor =[Uicolor Whitecolor]; the the //every button is different.98Cell.btnManInfo.tag = indexpath.section* -+Indexpath.row; About [Cell.btnmaninfo addtarget:self Action: @selector (Btnmaninfoclick:) forControlEvents: UIControlEventTouchUpInside]; - 101 returncell;102 }103 //Button Association method104- (void) Btnmaninfoclick: (ID) Sender the {106UIButton *tempbtn = (uibutton*) sender;107Nsinteger section = tempbtn.tag/ -;108Nsinteger row = tempbtn.tag% -;109 Switch(section) { the Case 0:111 if(Row = =0) { theNSLog (@"No. 0 Group No. 0 element");113 } the Else{ theNSLog (@"No. 0 Group 1th element"); the }117 Break;118 Case 1:119 if(Row = =0) { -NSLog (@"1th Group No. 0 Element");121 }122 Else{123NSLog (@"1th Group 1th element");124 } the Break;126 default:127 Break; - }129 } the 131 #pragmaMark-Proxy method the //the agent method that the listener row is selected to perform133- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) Indexpath134 {135NSLog (@"%@", Indexpath);136 }137 138 #pragmaMark-Layout139 //size of item $-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath141 {142 returnCgsizemake ( Max, Max);143 144 }145 //header size for each group146-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout referencesizeforheaderinsection: (nsinteger) section147 {148 returnCgsizemake ( -, -);149 Max }151 the #pragmaMark-Status bar153 //Hide the status bar154-(BOOL) Prefersstatusbarhidden155 {156 returnYES;157 }158 159- (void) didreceivememorywarning { the [Super didreceivememorywarning];161 //Dispose of any resources the can be recreated.162 }163 164 @end
IOS ui-Collection View (Uicollectionview)