iOS Development-Uicollectionview + examples

Source: Internet
Author: User

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)

    1. #pragma mark--Uicollectionviewdatasource
    1. Defines the number of Uicollectionviewcell to show
    2. -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
    3. {
    4. return 30;
    5. }   
    1. Define the number of sections to show
    2. -(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView
    3. {
    4. return 1;
    5. }   
  1. What each Uicollectionview shows
  2. -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
  3. {
  4. static NSString * Cellidentifier = @"Gradientcell";
  5. Uicollectionviewcell * cell = [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier forIndexPath: Indexpath];
  6. Cell.backgroundcolor = [Uicolor colorwithred: ((* indexpath.row)/255.0) Green: ((* indexpath.row)/255.0) Blue: ((30 * Indexpath.row)/255.0) alpha:1.0f];
  7. return cell;
  8. }   
    1. #pragma mark--uicollectionviewdelegateflowlayout
    1. //define the size of each uicollectionview    
    2. -  (cgsize) CollectionView: (uicollectionview *) Collectionview layout: ( uicollectionviewlayout*) Collectionviewlayout sizeforitematindexpath: (nsindexpath *) indexPath   
    3. {  
    4.     return  Cgsizemake (96, 100);   
    5. }   
    1. Define margin for each Uicollectionview
    2. -(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) Section
    3. {
    4. return Uiedgeinsetsmake (5, 5, 5, 5);
    5. }   
    1. #pragma mark--uicollectionviewdelegate
    1. //uicollectionview method called when selected   
    2. -(void) CollectionView: (uicollectionview *) collectionview  Didselectitematindexpath: (nsindexpath *) indexpath  
    3. {  
    4.     UICollectionViewCell * cell =  (uicollectionviewcell  *) [collectionview cellforitematindexpath:indexpath];  
    5.      cell.backgroundcolor = [uicolor whitecolor];  
    6. }   
    1. Returns whether this Uicollectionview can be selected
    2. -(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath
    3. {
    4. return YES;
    5. }

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

  1. -(ID) initWithFrame: (CGRect) frame
  2. {
  3. self = [super Initwithframe:frame];
  4. if (self)
  5. {
  6. //Load Collectioncell.xib file when initializing
  7. Nsarray *arrayofviews = [[NSBundle mainbundle] loadnibnamed:@"Collectioncell" owner:self Options:nil];
  8. //If the path does not exist, return nil
  9. if (Arrayofviews.count < 1)
  10. {
  11. return nil;
  12. }
  13. //If Xib view is not part of the Uicollectionviewcell class, return nil
  14. if (![ [Arrayofviews objectatindex:0] Iskindofclass:[uicollectionviewcell class])
  15. {
  16. return nil;
  17. }
  18. //Load NIB
  19. Self = [arrayofviews objectatindex:0];
  20. }
  21. return self;
  22. }


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.) )

    1. [Self.collectionview Registerclass:[collectioncell class] forcellwithreuseidentifier:@"CollectionCell"];


6. Declare the agent in ViewController.h

    1. @interface viewcontroller:uiviewcontroller<uicollectionviewdatasource,uicollectionviewdelegate>



7. Implementing proxy Methods in. m files

  1. Item count for each section
  2. -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
  3. {
  4. return 12;
  5. }
  6. -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
  7. {
  8. Collectioncell *cell = (Collectioncell *) [CollectionView dequeuereusablecellwithreuseidentifier:@]  Collectioncell "Forindexpath:indexpath";
  9. //Picture name
  10. NSString *imagetoload = [NSString stringwithformat:@"%d.png", Indexpath.row];
  11. //Load Picture
  12. Cell.imageView.image = [UIImage imagenamed:imagetoload];
  13. //Set Label text
  14. Cell.label.text = [NSString stringwithformat:@"{%ld,%ld}", (Long) Indexpath.row, (long) indexpath.section  ];
  15. return cell;
  16. }



8. Effect 10

Click on an item after the jump event similar to UITableView, implement proxy method

    1. -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath

Can not repeat the

iOS Development-Uicollectionview + examples

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.