IOS custom colletionView (pure code)

Source: Internet
Author: User

IOS custom colletionView (pure code)

Everyone said that colletionView and UITabbleView were brothers, and colletionView came out after IOS 6. colletionView and UITabbleView were indeed brothers, but you encountered many pitfalls when using them.
For example:
UICollectionView * colletionView = [[UICollectionView alloc] init]; initialize a colletionView. If you do this, you will fall into the trap. This is what the official documents give you.
So !!!! If you do this, it will collapse. You can only do this.

UICollectionViewFlowLayout * grid = [[UICollectionViewFlowLayout alloc] init]; grid. itemSize = CGSizeMake (80, 80); // set the size of the colletionView grid. sectionInset = UIEdgeInsetsMake (10.0, 10, 10); UICollectionView * colletionView = [[UICollectionView alloc] initWithFrame: self. view. frame collectionViewLayout: grid]; colletionView. delegate = self; colletionView. dataSource = self; [colletionView registerClass: [photo Cell class] forCellWithReuseIdentifier: @ "simpleCell"]; // your Cell init will not be called without adding it !!

OK is a successful instantiation of A colletionView. Like its brother, you need to set a proxy and set the data source. It has been set during instantiation.
Now implement his proxy and customize a colletionViewCell
Custom Cell to customize a Cell class to inherit UICollectionViewCell
. H

#import 
  
   @interface Cell : UICollectionViewCell@property (nonatomic,strong)UIImageView *image;@end
  

. M

@ Implementation Cell-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// change to our custom selected background view self. image = [[UIImageView alloc] init]; [self. image setFrame: CGRectMake (self. contentView. frame. size. width/2 + 20 + 5,-5, 20, 20)]; [self. image setBackgroundColor: [UIColor redColor]; self. image. layer. cornerRadius = self. image. frame. size. width/2; // set the border of the layer to rounded self. image. layer. masksToBounds = YES; // hide the boundary [self addSubview: self. image];} return self;} @ end
-(NSInteger) collect :( UICollectionView *) collectionView {return 1;}-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return 10;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellIdentifier = @ "simpleCell"; Cell * cell = (Cell *) [collectionView progress: cellIdentifier forIndexPath: indexPath]; if (cell = nil) {cell = [collectionView dequeueReusableCellWithReuseIdentifier: cellIdentifier forIndexPath: indexPath];} return cell;}-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {// -- standalone event}

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.