標籤:style blog http color io os ar for 檔案
建立CollectionCell模版:
1、建立類CollectionCell繼承自UICollectionViewCell
2、建立Xib,命名為CollectionCell.xib
a.選中CollectionCell.xib刪掉預設的View,從控制項中拖一個Collection View Cell(圖3)到畫布中,設定大小為95*116;
b.選中剛剛添加的Cell,更改類名為CollectionCell,4
c.在CollectionCell.xib的CollectionCell中添加一個ImageView和一個Label(圖5)
d.建立映射, 圖6,圖7
e.選中CollectionCell.m , 重寫init方法
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 初始化時載入collectionCell.xib檔案 NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil]; // 如果路徑不存在,return nil if (arrayOfViews.count < 1) { return nil; } // 如果xib中view不屬於UICollectionViewCell類,return nil if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; } // 載入nib self = [arrayOfViews objectAtIndex:0]; } return self; }
f.選中CollectionCell.xib 修改其identifier為CollectionCell。
======================================================
indexviewcontroller.h
#import <UIKit/UIKit.h>#import "CollectionCell.h"@interface indexviewcontroller : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@end
indexviewcontroller.m
#import "indexviewcontroller.h"@interface indexviewcontroller ()@end@implementation indexviewcontrollerUICollectionView *cv;- (void)viewDidLoad{ [super viewDidLoad]; [self loadcollectionview];}-(void)loadcollectionview{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; [flowLayout setItemSize:CGSizeMake(80, 100)];//設定cell的尺寸 [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];//設定其布局方向 cv=[[UICollectionView alloc]initWithFrame:CGRectMake(40,100,240,400) collectionViewLayout:flowLayout]; [cv registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"]; [cv setBackgroundColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.75]]; cv.delegate=self; cv.dataSource=self; [self.view addSubview:cv];}#pragma mark -- UICollectionViewDataSource//定義展示的UICollectionViewCell的個數-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 6;}//定義展示的Section的個數-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1;}//每個UICollectionView展示的內容-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString * CellIdentifier = @"CollectionCell"; UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor=[UIColor grayColor]; return cell;}#pragma mark --UICollectionViewDelegateFlowLayout//定義每個UICollectionView 的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(80, 100);}//定義每個UICollectionView 的 margin-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(5, 5, 5, 5);}#pragma mark --UICollectionViewDelegate//UICollectionView被選中時調用的方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor];}//返回這個UICollectionView是否可以被選擇-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
[IOS UICollectionView模版]