iOS6开发应用集合视图(UICollectionView)创建UICollectionViewCell子类单元格

如果还没有阅读前一部分文章:第三十一部分:应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格,建议先阅读之后,在看这一篇文章。

本文在前一篇文章基础之上,演示另外一种方法–通过创建UICollectionViewCell单元格子类来创建集合视图应用程序。在同一个Xcode项目中完成。

本示例程序最终运行界面的效果如下所示:

创建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属性,设置其文本内容。

再次运行应用程序,和之前的效果基于一致,只是这一次我们修改了单元格的背景色。

原文地址:https://www.cnblogs.com/tuncaysanli/p/2821850.html