IOS UICollectionView handwritten code implementation steps

Source: Internet
Author: User

IOS UICollectionView handwritten code implementation steps
IOS UICollectionView handwritten code implementation steps

//

// ViewController. h

// CollectionView handwritten code

//

// Created by yangxiuying on 14/11/28.

// Copyright (c) 2014 lanjiying. All rights reserved.

//


# Import


@ Interface ViewController: UIViewController

{

NSString * identifier;

}

//

// ViewController. m

// CollectionView handwritten code

//

// Created by yangxiuying on 14/11/28.

// Copyright (c) 2014 lanjiying. All rights reserved.

//


# Import "ViewController. h"


@ Interface ViewController ()



@ End

@ Implementation ViewController


-(Void) viewDidLoad {

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

Identifier = @ "cell ";

// Initialize layout

UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];

[FlowLayout setScrollDirection: UICollectionViewScrollDirectionVertical];

// UIEdgeInsets top = {15, 10, 15, 5 };

// [FlowLayout setSectionInset: top];

UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake (0, 30,320,480) collectionViewLayout: flowLayout];

// Register a cell

[CollectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: identifier];

// Set proxy

CollectionView. delegate = self;

CollectionView. dataSource = self;

[Self. view addSubview: collectionView];

}

# Pragma mark-collectionView delegate

// Set partitions



-(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView

{

Return 1;

}

// Set the element size box

-(UIEdgeInsets) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex :( NSInteger) section

{

UIEdgeInsets top = {30, 15, 30, 15 };

Return top;

}

// Number of elements on each partition

-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section

{

Return 24;

}

// Set the Element Content

-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath

{

UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: identifier forIndexPath: indexPath];

[Cell sizeToFit];

Cell. backgroundColor = [UIColor greenColor];

Return cell;

}

// Set the cell width

// Set the element size

-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath :( NSIndexPath *) indexPath {

Return CGSizeMake (140,160 );

}


Related Article

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.