IOS: Create a subclass of UICollectionView

Source: Internet
Author: User

IOS: Create a subclass of UICollectionView

The creation of UICollectionView is basically the same as that of UITableView.

First, create a subclass that inherits from UICollectionView

Then set some attributes in the initialization method.

 

-(Id) initWithFrame :( CGRect) frame {UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout. minimumInteritemSpacing = 0; // The column spacing is flowLayout. minimumLineSpacing = 0; // line spacing self = [super initWithFrame: frame collectionViewLayout: flowLayout]; if (self) {// hide the slider self. showsHorizontalScrollIndicator = NO; self. showsVerticalScrollIndicator = NO; // sets the proxy self. delegate = self; self. dataSource = self; // set the background color (black by default) self. backgroundColor = [UIColor whiteColor]; // register the cell [self registerClass: [YSStudentStatusCell class] forCellWithReuseIdentifier: identify];} return self ;}

The Protocol method of collectionView is basically the same as that of tableView. The main difference is that cell creation and header view creation.

 

First look at cell Creation

 

 

 

// Create a cell

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

{

ModelStudent * model = self. dataArray [indexPath. item];

// Subclass cell

YSStudentStatusCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: identify forIndexPath: indexPath];

[Cell configureWithModel: model. StuUserInfo];

Return cell;

}

 

The initialization method for cell subclass creation in collectionView is as follows:

 

- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self initView];    }    return self;}

To create a collectionView header view, you must first register the header view. Register a demo. I generally write it in a cell registration module.

 

 

// Register the collection header view [self registerClass: [UICollectionReusableView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @ "Identifierhead"];
How to Create a header view Protocol

 

 

// Create the group's header view-(UICollectionReusableView *) collectionView :( UICollectionView *) collectionView detail :( NSString *) kind atIndexPath :( NSIndexPath *) indexPath {UICollectionReusableView * headView = [collectionView detail: kind withReuseIdentifier: @ "Identifierhead" forIndexPath: indexPath]; headView. backgroundColor = [UIColor blueColor]; return headView ;}

 

 

In addition, the use of flowLayout

 

FlowLayout is the layout attribute of collectionView. Different flowLayout settings can be used to load different layout patterns of collectionView.


 

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.