UICollectionView是iOS6新引進的API,用于展示集合視圖,布局更加靈活,其用法類似于UITableView。而UICollectionView、UICollectionViewCell與UITableView、UITableViewCell在用法上有相似的也有不同的,下面是一些基本的使用方法:
對于UITableView,僅需要UITableViewDataSource,UITableViewDelegate這兩個協議,使用UICollectionView需要實現UICollectionViewDataSource,
UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個協議,這是因為UICollectionViewDelegateFlowLayout實際上是UICollectionViewDelegate的一個子協議,它繼承了UICollectionViewDelegate,它的作用是提供一些定義UICollectionView布局模式的函數,一些常用函數如下:
//設定指定Cell的尺寸-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;//設定collectionView(指定區(qū))的邊距-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;//設定指定區(qū)內Cell的最小行距-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;//設定指定區(qū)內Cell的最小間距-(CGFloat)collectionView:(UICollectionView *)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
接來下我們開始自定義一個UICollectionViewCell。首先創(chuàng)建一個單獨的類DoctorItemViewCell繼承自UICollectionViewCell。
實例化一個UICollectionView時,為了能夠使用cell,必須得使用下面的方法進行Cell類的注冊:
- (void)registerClass: (__unsafe_unretained Class)forCellWithReuseIdentifier: (NSString *)forCellWithReuseIdentifier:
而對于tableView來說并不需要在實例化時使用方法對Cell類進行注冊。對于這個注冊函數需要注意一點:registerClass:的參數一定要是我們所使用的那個UICollectionViewCell類,否則就會報錯!因此我們需要這么寫:
//docCollectionView為自定義的UICollectionView的實例對象//DoctorItemViewCell為自定義的UICollectionViewCell類[docCollectionView
registerClass:[DoctorItemViewCell class]
forCellWithReuseIdentifier:@"cell"];
UICollectionView的UICollectionViewDataSource和UICollectionViewDelegate協議的方法作用與UITableView的兩個協議方法作用類似,例如:
//定義展示的UICollectionViewCell的個數
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//定義展示的UITableViewCell的個數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//每個UICollectionView展示的內容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//每個UITableView展示的內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
自定義UICollectionViewCell時,在
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
方法里我們僅需寫入:
// DoctorItemViewCell為自定義的UICollectionViewCell類
DoctorItemViewCell
*cell = (DoctorItemViewCell *)[collectionView
dequeueReusableCellWithReuseIdentifier:indentifier
forIndexPath:indexPath]
而不需要像UITableViewCell那樣判斷Cell是否已被創(chuàng)建,這是因為在自定義UICollectionViewCell時有一個極其重要的方法:
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
//initialize code...
}
returnself;
}
這個方法是在初始化自定義的cell時系統自動調用的,所以不需要判斷cell是否已經被創(chuàng)建。而對于UITableViewCell來說初始化方法并不唯一,所以我們需要在指定它的初始化方法。
此外還需注意的一點是,自定義的UICollectionViewCell沒有UITableViewCell如下類似的方法
DoctorItemViewCell *cell = (DoctorItemViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier];
而是有多一個indexPath參數的方法:
DoctorItemViewCell
*cell = (DoctorItemViewCell *)[collectionView
dequeueReusableCellWithReuseIdentifier:indentifier
forIndexPath:indexPath];
在自定義UICollectionViewCell時需要注意的是,在cell里添加的那些信息變量是需要寫在.h文件中作為公共的變量,否則在UICollectionView.m文件的
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
方法中是無法將cell所需的數據傳給cell并展示在視圖中的,因為collectionViewCell默認的也是惟一的初始化方法就是initWithFrame。所以根本無法在初始化方法中傳遞參數。而tableView就不用這樣做,這是因為對于UICollectionViewCell類來說,它本身是不包含像UITableViewCell所含的imageView和titleLabel這樣的控件屬性的,所以自定義UICollectionViewCell時,需要將Cell所需要的控件屬性放在UICollectionViewCell.h文件中作為外部可訪問的公共屬性。
最后附上參考代碼:
//DoctorItemViewCell.h文件#import
#import "DoctorCollectionViewController.h"@interfaceDoctorItemViewCell :
UICollectionViewCell//自定義的控件屬性@property (strong, nonatomic)UIImageView
*imageView;
@property (strong, nonatomic)UILabel *nameLabel;
@end
//DoctorItemViewCell.m文件
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kInterval, kInterval, 85, 105)];
self.imageView.image = [UIImage imageNamed:@"appCover"];
self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageView.frame)+kInterval, 8, 70, 22)];
self.nameLabel.font = [UIFont systemFontOfSize:21];
self.sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame)+kInterval, 11, 16, 16)];
self.sexLabel.font = [UIFont systemFontOfSize:14];
}
returnself;
}
collectionViewCell的創(chuàng)建方法:
-?(UICollectionViewCell?*)collectionView:(UICollectionView?*)collectionView?cellForItemAtIndexPath:(NSIndexPath?*)indexPath?{
staticNSString?*indentifier?=?@"cell";
DoctorItemViewCell?*cell?=?(DoctorItemViewCell?*)[collectionView?dequeueReusableCellWithReuseIdentifier:indentifier?forIndexPath:indexPath];
cell.layer.cornerRadius?=?5;
cell.backgroundColor?=?[UIColor?whiteColor];
return?cell;
}