This chapter first introduces Uicollectionview and its common methods, and then combines an example to learn how to use Uicollectionview.
The Uicollectionview and Uicollectionviewcontroller classes are IOS6 's newly introduced APIs for presenting collection views, with a more flexible layout that enables multi-column layouts, similar to UITableView and Uitableviewcontroller class.
Using Uicollectionview must implement Uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout these three agreements.
here are some commonly used methods. (only common, others can see the relevant API)
- #pragma mark--Uicollectionviewdatasource
- Defines the number of Uicollectionviewcell to show
- -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
- {
- return 30;
- }
- Define the number of sections to show
- -(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView
- {
- return 1;
- }
- What each Uicollectionview shows
- -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
- {
- static NSString * Cellidentifier = @"Gradientcell";
- Uicollectionviewcell * cell = [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier forIndexPath: Indexpath];
- Cell.backgroundcolor = [Uicolor colorwithred: ((* indexpath.row)/255.0) Green: ((* indexpath.row)/255.0) Blue: ((30 * Indexpath.row)/255.0) alpha:1.0f];
- return cell;
- }
- #pragma mark--uicollectionviewdelegateflowlayout
- //define the size of each uicollectionview
- - (cgsize) CollectionView: (uicollectionview *) Collectionview layout: ( uicollectionviewlayout*) Collectionviewlayout sizeforitematindexpath: (nsindexpath *) indexPath
- {  
- return Cgsizemake (96, 100);
- }
- Define margin for each Uicollectionview
- -(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) Section
- {
- return Uiedgeinsetsmake (5, 5, 5, 5);
- }
- #pragma mark--uicollectionviewdelegate
- //uicollectionview method called when selected
- -(void) CollectionView: (uicollectionview *) collectionview Didselectitematindexpath: (nsindexpath *) indexpath
- {
- UICollectionViewCell * cell = (uicollectionviewcell *) [collectionview cellforitematindexpath:indexpath];
- cell.backgroundcolor = [uicolor whitecolor];
- }
- Returns whether this Uicollectionview can be selected
- -(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath
- {
- return YES;
- }
The following is a concrete example of this. (Examples come from the web.) However, it is obtained through a third party and cannot be linked. Also hope forgive me. )
The advent of IOS CollectionView is a great benefit, no longer using TableView to define complex multi-column tables, similar to table, except that the cell must be added by itself, with no default mode
Since the CollectionView does not have a default cell layout, it is generally easy and fast to customize
One, custom cell
1, new class Collectioncell inherit from Uicollectionviewcell
2, New Xib, named Collectioncell.xib
A. Select Collectioncell.xib to delete the default view, drag a collection view Cell from the control (Figure 3) to the canvas, and set the size to 95*116;
B. Select the cell that you just added, change the class name to collectioncell,4
C. Add a ImageView and a label to the Collectioncell of Collectioncell.xib (Figure 5)
D. Creating a map, Figure 6, Figure 7
E. Select collectioncell.m, overriding the Init method
- -(ID) initWithFrame: (CGRect) frame
- {
- self = [super Initwithframe:frame];
- if (self)
- {
- //Load Collectioncell.xib file when initializing
- Nsarray *arrayofviews = [[NSBundle mainbundle] loadnibnamed:@"Collectioncell" owner:self Options:nil];
- //If the path does not exist, return nil
- if (Arrayofviews.count < 1)
- {
- return nil;
- }
- //If Xib view is not part of the Uicollectionviewcell class, return nil
- if (![ [Arrayofviews objectatindex:0] Iskindofclass:[uicollectionviewcell class])
- {
- return nil;
- }
- //Load NIB
- Self = [arrayofviews objectatindex:0];
- }
- return self;
- }
F. Select Collectioncell.xib to modify its identifier to Collectioncell.
Second, the definition of uicollectionview;
1. Drag a collection view to the specified Viewcontroller view
2. Connect DataSource and delegate, and create a map named CollectionView
3. Select the CollectionView ruler to change the width and height of the cell size to the same 95*116 as the custom cell, figure 8
4. Check the properties of CollectionView, you can modify its properties, such as vertical sliding, or horizontal sliding, select vertical or Horizontal
5, select Collectionviewcell, modify class, Inherit from Collectioncell
5, in the Viewdidload method declaration cell class, added in the Viewdidload method, this sentence does not declare, will not load, program crashes
Where Collectioncell is the identity of the cell (the previous steps have already been defined.) )
- [Self.collectionview Registerclass:[collectioncell class] forcellwithreuseidentifier:@"CollectionCell"];
6. Declare the agent in ViewController.h
- @interface viewcontroller:uiviewcontroller<uicollectionviewdatasource,uicollectionviewdelegate>
7. Implementing proxy Methods in. m files
- Item count for each section
- -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
- {
- return 12;
- }
- -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
- {
- Collectioncell *cell = (Collectioncell *) [CollectionView dequeuereusablecellwithreuseidentifier:@] Collectioncell "Forindexpath:indexpath";
- //Picture name
- NSString *imagetoload = [NSString stringwithformat:@"%d.png", Indexpath.row];
- //Load Picture
- Cell.imageView.image = [UIImage imagenamed:imagetoload];
- //Set Label text
- Cell.label.text = [NSString stringwithformat:@"{%ld,%ld}", (Long) Indexpath.row, (long) indexpath.section ];
- return cell;
- }
8. Effect 10
Click on an item after the jump event similar to UITableView, implement proxy method
- -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath
Can not repeat the
iOS Development-Uicollectionview + examples