應用集合視圖(UICollectionView)-創建UICollectionViewCell子類單元格

創建UICollectionViewCell子類單元格

創建一個定製的UICollectionViewCell子類是另外一種方法,對單元格的樣式和行爲可以提供更大的控制程度。

首先,我們創建一個UICollectionViewCell的子類。選擇File > New > File…菜單項,然後選擇Cocoa Touch節點下的Objective-C Class 模板。

進一步設置類名稱SimpleClass,設置爲UICollectionViewCell的子類。

這樣,將創建2個文件,分別爲頭文件和實現文件。 
接下來,我們創建一個新的視圖文件,和前面的操作方式基本一致,設置文件名稱爲SimpleLableCell(之前的視圖文件爲NibCell.xib)。

和前面的操作方式一樣,我們刪除默認的View視圖,添加Collection View Cell對象到畫布中。另外設置背景色爲綠色,尺寸大小爲100×100,當然還放置一個Label標籤。

打開SimpleLabelCell.xib文件,在Indentity inspector面板窗口,設置Class屬性爲SimpleCell,這個是我們前面創建的UICollectionViewCell子類。

在Attributes inspector面板窗口,設置Identifier屬性爲simpleCell,後面的代碼中會用到這一標識符。

現在,我們建立SimpleLableCell.xib視圖文件中Label標籤到視圖控制器中的輸出口,輸出口名爲titleLabel。 
#import <UIKit/UIKit.h>
@interface SimpleCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@end

打開SimpleCell.m實現文件,我們需要修改默認的initWithFrame:方法。 
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SimpleLabelCell" owner:self options: nil];
if(arrayOfViews.count < 1){return nil;}
if(![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]){
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}

對上面的代碼解釋一下,首先調用父類的initWithFrame:方法,接着加載xib文件到NSArray 數組中,如果數組中沒有元素,則表示有問題,返回nil。否則,我們獲取其中第一個元素,這個元素對象應該爲UICollectionViewCell類,如果不是,則表示出現問題,返回nil。如果一切正常,則獲取第一個對象,並賦值給self,這是因爲這個對象本身是UICollectionViewCell 對象實例,最後返回。 
現在,我們已經創建好了UICollectionView子類,我們需要引入這個子類,並註冊到集合視圖中。在視圖控制器SimpleViewController.m的頂部,添加如下#import指令。 
#import "SimpleCell.h"

註釋viewDidLoad方法中註冊nib的代碼,並添加如下代碼註冊單元格子類: 
// 在Collection View 中進行Cell類的註冊
//UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
//[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"simpleCell"];

[self.collectionView registerClass:[SimpleCell class] forCellWithReuseIdentifier:@"simpleCell"];

通過在集合視圖中註冊SimpleCell類,並設置重用標識符爲simpleCell。在後續代碼中,我們要求集合視圖使用這一標識符取出單元格對象時,它將返回一個SimpleCell的對象實例。 
這表示我們需要更新collectionView:cellForItemAtIndexPath:方法,將之前的代碼註釋掉,然後添加新的代碼。 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/*NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"simpleCell";
// 從隊列中取出一個Cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:10];
titleLabel.text = cellData;
return cell;*/

NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
static NSString *cellIdentifier = @"simpleCell";
SimpleCell *cell = (SimpleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.titleLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}

代碼中,我們請求集合視圖從隊列中取出一個UICollectionViewCell單元格實例,並轉換爲SimpleCell子類,接着訪問其titleLabel屬性,設置其文本內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章