iOS Development Pure Code creation Uicollectionview

Source: Internet
Author: User

Ext.: http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html

iOS Development Pure Code creation Uicollectionview

Accustomed to the use of xib and storyboard to create Uicollectionview Project Engineering Partners, need to transform the use of pure code to achieve, to avoid touching more walls, you need to carefully understand the creation of the Uicollectionview process. Creating a Uicollectionview is more complex than creating a uitableview, and it is also a relatively peculiar way of initializing. Here's how to create a Uicollectionview using pure code.

Tools/Materials
    • MAC OS X operating system
    • Xcode compiler
Method/Step
  1. 1

    Create project and view Controller

          Create a project Uicollectionview and build a new uiviewcontroller. Select Project, right-click-new File ... Select "Cocoa Touch Class"-next, give a reasonable name Viewcontroller, then Next complete.

          include #import "ViewController.h" in the APPDELEGATE.M file. Add code:

    Uinavigationcontroller *NAVC = [[Uinavigationcontroller alloc]initwithrootviewcontroller:[[ Viewcontroller Alloc]init]];

        Self.window.rootViewController = navc;//Sets the NAVC to the root view controller.

    Modify the display style of Viewcontroller, perform compilation, run, effect.

  2. 2

    Create custom Uicollectionviewcell

          Select project, right-click-new File ... Select "Cocoa Touch class"-next, select inherit from Uicollectionviewcell class, give a reasonable name Collectionviewcell, then Next complete.

          1, customize the controls you want, such as Uiimageview:

    @property (nonatomic, strong) Uiimageview *imgview;

          2, initialize the control, implemented in method-(ID) initWithFrame: (CGRect) Frame:

    self.imgview = [[Uiimageview Alloc]initwithframe:cgrectmake (0, Max, Max)];

            Self.imgView.backgroundColor = [Uicolor grouptableviewbackgroundcolor];

            [self addSubview:self.imgView];

  3. 3

    create Uicollectionview and add proxies

          1, add event agent and data source Agent <UICollectionViewDataSource,UICollectionViewDelegate> in ViewController.h.

          2, create Uicollectionview in VIEWCONTROLLER.M. Need to use Uicollectionviewflowlayout to create, using method -  (Instancetype) initWithFrame: (CGRect) Frame collectionviewlayout :(uicollectionviewlayout *) layout; If you only use the normal Init method, it is not possible.

          4, delegate authorization and add to view.

        self.collectionview.delegate = self;

        self.collectionView.dataSource = self;

        [Self.view AddSubview:self.collectionView];

  4. 4

    add Uicollectionviewcell to Uicollectionview

          1, register Collectionviewcell, add cell needs to be implemented here. Method: -(void) RegisterClass: (Class) Cellclass forcellwithreuseidentifier: (NSString *) identifier;

          2, adding proxy methods

    //define the number of Uicollectionviewcell displayed

    -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section;

     

     

    //define the number of sections displayed

    -(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView;

     

     

    //What each Uicollectionview shows

    -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) Indexpath;

  5. 5

    setting properties in Uicollectionview

    // Define the size of each uicollectionview (return cgsize: Width and height)

    -(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout :(uicollectionviewlayout*) collectionviewlayout sizeforitematindexpath: (Nsindexpath *) IndexPath;

     

     

    //define the spacing for each uicollectionview (return uiedgeinsets: Top, left, bottom, right)

    -(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) section;

    //define the longitudinal spacing for each uicollectionview

    -(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section;

     

     

    The method called when//uicollectionview is selected

    -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath;

    end
Code
    • Https://github.com/cjq002/UICollectionView-Pure-code.git

    • The code runs the effect.

      END

iOS Development Pure Code creation Uicollectionview

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.