1. What is a collection view
After iOS6.0 , Apple introduced a new view that inherits from the Uiscrollview , Uicollectionview , also known as the Collection View . Common with UITableView as the two views commonly used in development, often appearing as the main interface of the project
2. Create Uicollectionview
The realization of 1> Uicollectionview
u Icollectionview is implemented with tableview is not the same place layout a little more complicated , need uicollectionviewlayout the class to describe the layout of the view's view. What we commonly use in our projects is the system-provided uicollectionviewflowerlayout class, you can also customize uicollectionviewlayout
2> Creating uicollectionviewflowerlayout
1 //1. Define the style of the CollectionView2 3Self.myflowlayout = [UicollectionviewflowlayoutNew];4 //Setting Properties5 //the size of the given item6Self.myFlowLayout.itemSize = Cgsizemake ((Kwidth- +) /3, Kwidth/3);7 //minimum gap between two item (vertical scrolling)8Self.myFlowLayout.minimumInteritemSpacing =Ten;9 //minimum gap (horizontal scrolling) between each two itemTenSelf.myFlowLayout.minimumLineSpacing =Ten; One //Set Scroll Direction ASelf.myFlowLayout.scrollDirection = uicollectionviewscrolldirectionvertical;//Vertical Direction - //self.myFlowLayout.scrollDirection = uicollectionviewscrolldirectionhorizontal;//Horizontal Direction - //set the inner margin of the view (top left and bottom right) theSelf.myFlowLayout.sectionInset = Uiedgeinsetsmake ( -,Ten,Ten,Ten); - - //Layout Head View dimensions -Self.myFlowLayout.headerReferenceSize = Cgsizemake (0, -); + - //layout tail View dimensions +Self.myFlowLayout.footerReferenceSize = Cgsizemake (0, -);
3> Initialize uicollectionview
1 // 2. Layout CollectionView 2 // create an object and specify a style 3 Self.collectionview = [[Uicollectionview alloc] initWithFrame:self.bounds collectionviewlayout: Self.myflowlayout]; 4 5 Self.collectionView.backgroundColor = [Uicolor cyancolor]; 6 7 [Self addSubview:self.collectionView];
4> setting up Agents
// set up proxy Self.rootView.collectionView. delegate = self ; = self;
5> Register Cell
// First step: Register the cell class] Forcellwithreuseidentifier:identifier_cell];
6> Proxy Method
① uicollectionview and UITableView have two proxy methods that must be implemented
Returns the number of Item
1 // set the number of item per partition 2 -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section3{4 return; 5 }
Specify the style for each Item
1 //returns a Cell object for each item2-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath3 {4Rootcell *cell =[CollectionView Dequeuereusablecellwithreuseidentifier:identifier_cell Forindexpath:indexpath];5 6Cell.backgroundcolor =[Uicolor Redcolor];7 8 if(0==indexpath.section) {9Cell.photoImage.image = [UIImage imagenamed:@"1"];Ten } One if(1==indexpath.section) { ACell.photoImage.image = [UIImage imagenamed:@"2"]; - } - if(2==indexpath.section) { theCell.photoImage.image = [UIImage imagenamed:@"3"]; - } - - returncell; +}
Rootcell for custom cells
② choosing an implemented proxy method
Uicollectionview has a number of proxy methods that you can choose to implement, such as:
- To set the number of partitions:
1 // returns the number of groups 2 -(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView3 {4 return 3 ; 5 }
1 // Click on item 2 -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: ( Nsindexpath *) Indexpath3{4 new]; 5 6 [Self.navigationcontroller PUSHVIEWCONTROLLER:SECONDVC animated:yes]; 7 }
- Returns the style of the head, tail view
Uicollectionview can not directly specify the head and tail view as the UITableView , need to register use , the biggest benefit is to add the reuse mechanism .
1 //Register Head View2[Self.rootView.collectionView Registerclass:[headerreusableviewclass] Forsupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:@"Headerview"];3 4 //Register Tail View5[Self.rootView.collectionView Registerclass:[uicollectionreusableviewclass] Forsupplementaryviewofkind:uicollectionelementkindsectionfooter Withreuseidentifier:@"Footerview"];6 //return to head view and tail view7-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath8 {9 //judge whether the head view or the tail viewTen if([Kind Isequaltostring:uicollectionelementkindsectionheader]) { One AHeaderreusableview *headerview = [CollectionView dequeuereusablesupplementaryviewofkind:kind withReuseIdentifier:@"Headerview"Forindexpath:indexpath]; - - //Headerview.backgroundcolor = [Uicolor redcolor]; the - Switch(indexpath.section) { - Case 0: -HeaderView.headerLabel.text =@"Student Period"; + Break; - Case 1: +HeaderView.headerLabel.text =@"Life Time"; A Break; at Case 2: -HeaderView.headerLabel.text =@"Programmer"; - Break; - default: - Break; - } in returnHeaderview; - } to +Uicollectionreusableview *footerview = [CollectionView dequeuereusablesupplementaryviewofkind: Uicollectionelementkindsectionfooter Withreuseidentifier:@"Footerview"Forindexpath:indexpath]; -Footerview.backgroundcolor =[Uicolor Lightgraycolor]; the * returnFooterview; $}
:
3. Layout Protocol
Layout protocol: Uicollectionviewdelegateflowlayout , which is an extension to uicollectionviewdelegate
The Uicollectionview of IOSDay34