IOS UICollectionView pure code, no xib

Source: Internet
Author: User

IOS UICollectionView pure code, no xib
//

1)You must use the following method to register the Cell class:

// -(Void) registerClass: forCellWithReuseIdentifiEr:

// -(Void) registerClass: forSupplementaryViewOfKiNd: withReuseIdentifier:

// -(Void) registerNib: forCellWithReuseIdentifiEr:

// -(Void) registerNib: forSupplementaryViewOfKiNd: withReuseIdentifier:


-(Void) viewDidLoad

{

// Initialization

UICollectionViewFlowLayoUt * flowLayout = [[UICollectionViewFlowLayoUtalloc] init];

[FlowLayout setScrollDirection: UICollectionViewScrollDiRectionVertical];

Self. collectionView = [[UICollectionViewalloc] initWithFrame: CGRectMake (0, kNavHeight, kDeviceWidth, kDeviceHeight-kNavHeight * 2-ktabbarheight20) collectionViewLayout: flowLayout];

// Register

[Self. collectionView registerClass: [VideoCell class] forCellWithReuseIdentifiEr: @ "cell"];

// Set proxy

Self. collectionView. delegate = self;

Self. collectionView. dataSource = self;

[Self. view addSubview: self. collectionView];

}

# Pragma mark-collectionView delegate

// Set partitions

-(NSInteger) numberOfSectionsInCollecTionView :( UICollectionView *) collectionView

{

Return 1;

}


// Number of elements in each partition

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

{

Return 24;

}


// Set the Element Content

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

{

Static NSString * identify = @ "cell ";

VideoCell * cell = [collectionView dequeueReusableCellWithREuseIdentifier: identify forIndexPath: indexPath];

[Cell sizeToFit];

If (! Cell ){

}

VideoModel * model = [self. videoModels objectAtIndex: indexPath. row];

NSURL * url = [NSURL URLWithString: model. videoImgURL];

[Cell. imgView setImageWithURL: url];

Cell. titleLbale. text = model. videoTitle;

Return cell;

}


// Set the element size box

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

{

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

Return top;

}


// Set the top size

-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout referenceSizeForHeaderInSection :( NSInteger) section

{

CGSize size = {0, 0 };

Return size;

}

// Set the element size

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

{

Return CGSizeMake (240, (kDeviceHeight-kNavHeight * 2-ktabbarheight20)/4.0 );

}


// Click an element to trigger the event

-(Void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath:( NSIndexPath *) indexPath

{

NSLog (@ "% @", indexPath );

DetailVideoViewControlleR * detailVC = [[DetailVideoViewControlleRalloc] init];

[Self. navigationController pushViewController: detailVCanimated: YES];

}

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.