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];
}