应用集合视图(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属性,设置其文本内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值