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}