How to use Uicollectionview

Source: Internet
Author: User

1. Compliance with agreements

<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

2. Create

Uicollectionviewflowlayout * layout = [[Uicollectionviewflowlayout alloc]init];
layout.minimuminteritemspacing = 10; The spacing between minimum item
layout.minimumlinespacing = 10;//Minimum line spacing
CollectionView = [[Uicollectionview alloc]initwithframe:cgrectmake (0, 0, WIDTH, height-64-49) Collectionviewlayout: Layout];
Collectionview.delegate = self;
Collectionview.datasource = self;

Important

1>, if it is a code-defined cell, it should be registered with the following method

[CollectionView Registerclass:[piccollectionviewcell class] forcellwithreuseidentifier:@ "CC"];

2>, if the cell defined by Xib is to be used

[CollectionView registernib:[uinib nibwithnibname:@ "Piccollectionviewcell" Bundle:[nsbundle MainBundle]] forcellwithreuseidentifier:@ "CC"];

3, return the number of item

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
{
return datasourse.count;
}

4. Cell multiplexing

Xib and code both use the following methods

Piccollectionviewcell * cell = [CollectionView dequeuereusablecellwithreuseidentifier:@ "CC" ForIndexPath:indexPath];

5. Important Protocol Method

1> returns the size of item, the system automatically sets the number of items displayed per row according to the size of item (Layout.size method can be used)

-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath
{
Cgsize size = Cgsizemake (90, 80);
return size;
}

2>//Returns whether this Uicollectionview can be selected

-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath

{ return YES;

}

How to use 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.