先看看apple官网简述:

registerNib:forCellWithReuseIdentifier:
Register a nib file for use in creating new collection view cells. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier
Parameters
nib
The nib object containing the cell object. The nib file must contain only one top-level object and that object must be of the type UICollectionViewCell.
identifier
The reuse identifier to associate with the specified nib file. This parameter must not be nil and must not be an empty string.
Discussion
Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerClass:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically. If you previously registered a class or nib file with the same reuse identifier, the object you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib file from the specified reuse identifier. Availability
Available in iOS 6.0 and later.
See Also
– registerClass:forCellWithReuseIdentifier:
– dequeueReusableCellWithReuseIdentifier:forIndexPath:
Declared In
UICollectionView.h
registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
Registers a nib file for use in creating supplementary views for the collection view. - (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier
Parameters
nib
The nib object containing the view object. The nib file must contain only one top-level object and that object must be of the type UICollectionReusableView.
kind
The kind of supplementary view to create. The layout defines the types of supplementary views it supports. The value of this string may correspond to one of the predefined kind strings or to a custom string that the layout added to support a new type of supplementary view. This parameter must not be nil.
identifier
The reuse identifier to associate with the specified nib file. This parameter must not be nil and must not be an empty string.
Discussion
Prior to calling the dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerClass:forSupplementaryViewOfKind:withReuseIdentifier: method to tell the collection view how to create a supplementary view of the given type. If a view of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a view object automatically. If you previously registered a class or nib file with the same element kind and reuse identifier, the class you specify in the viewClass parameter replaces the old entry. You may specify nil for nib if you want to unregister the class from the specified element kind and reuse identifier. Availability
Available in iOS 6.0 and later.
See Also
– registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
– dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
Declared In
UICollectionView.h

https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/registerClass:forCellWithReuseIdentifier:

https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/registerNib:forCellWithReuseIdentifier:

registerNib:

在此之前调用dequeueReusableCellWithReuseIdentifier:forIndexPath:集合视图的方法,则必须使用此方法或通过registerClass:forCellWithReuseIdentifier:方法告诉集合视图如何创建指定类型的新Cell。如果指定类型的Cell不是当前中重用队列,集合视图使用所提供的信息来自动创建新Cell对象。

如果您之前注册的Class或Nib具有相同标识符的重用,你在Nib参数中指定的对象替换旧的条目。如果你想从指定的重用标识符注销nib文件您可以为Nib标记为nil。

registerClass:

在此之前调用dequeueReusableCellWithReuseIdentifier:forIndexPath:集合视图的方法,则必须使用此方法或registerNib:forCellWithReuseIdentifier:方法告诉集合视图如何创建指定类型的新单元。如果指定类型的单元不是当前中重用队列,集合视图使用所提供的信息来自动创建新的单元格对象。 

如果您之前注册的具有相同标识符的重用类或笔尖文件,您在cellClass参数中指定的类取代旧的条目。如果你想从指定的重用标识符注销的类,你可以为cellClass指定为nil。

  

具体实例如下:

MARK: 这里是在viewDidLoad 里registerNib:......

#pragma mark - View management
- (void)viewDidLoad
{
// Build collection view
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor]; // Register cell and views
[_collectionView registerNib:[RootCell cellNib] forCellWithReuseIdentifier:RootCell_ID];
}

RootCell.m:

//@interface RootCell : UICollectionViewCell

#pragma mark - Utils
static UINib *cellNib;
+ (UINib*)cellNib
{
if (cellNib)
return cellNib; // Build cell nib
cellNib = [UINib nibWithNibName:RootCell_XIB
bundle:nil]; return cellNib;
}

获取Cell对象:cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
RootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RootCell_ID forIndexPath:indexPath]; return cell;
}

这里如果在cellForItemAtIndexPath: 处处理注册事件也可以,不过不建议这么使用

TODO:

static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
nibsRegistered = YES;
} CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];

ios registerNib: and registerClass:的更多相关文章

  1. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  2. iOS之UICollectionView详解

    UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UIColle ...

  3. iOS Autolayout 在tableView scrollView 适用 学习

    1  如何自动适应cell的高度 autolayout  里面 使用 systemLayoutSizeFittingSize 方法 (系统通过 已知的完整的Constraints和view的属性来计算 ...

  4. When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath

    The most important difference is that the forIndexPath: version asserts (crashes) if you didn't regi ...

  5. iOS---UICollectionView Class Reference---UICollectionView 类参考文档

    UICollectionView 类: Inherits from UIScrollView : UIView : UIResponder : NSObject Conforms to NSCodin ...

  6. 为什么Tableviewcell创建时可以不判空

    dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别: 前者不必向tableV ...

  7. uicollectionview registerclass vs registernib

    当cell是用代码实现的时候用registerclass,当cell是用xib文件实现的时候用registernib

  8. iOS UITableView 引起的崩溃问题

    其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...

  9. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

随机推荐

  1. hdfs里的文件下载HDFS之fsimage、metadata、edits、fstime(二十七)

    首先,要有这个观念,元数据信息(fsimage + editslog). fsimage是在磁盘 metadata是在内存 ********************fsimage把内存的,序列化到磁盘 ...

  2. Android实例-路径信息及文件和文件夹的操作(XE8+小米2)

    结果: GetTempFileName:/storage/sdcard0/Android/data/com.embarcadero.Project1/files/tmp/tmp.iQIip24407 ...

  3. hdoj 2098 分拆素数和

    分拆素数和 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. hdoj 2051 Bitset

    Bitset Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. [OC Foundation框架 - 14] NSNull

    在NSDictionary中,nil代表结束,允许存入 使用NSNull代替   int main(int argc, const char * argv[]) { @autoreleasepool ...

  6. Javascript(jQuery)中绑定页面上所有按钮点击事件的几种方式

    方法一:使用document对象查找所有的按钮 [javascript] view plain copy 在CODE上查看代码片派生到我的代码片 //按照dom的方式添加事件处理 function B ...

  7. 【Away3D代码解读】(一):主要类及说明

    在深入解读Away3D的代码之前,需要对其有个大概的认识.本节主要列出Away3D中常用的类,并附上说明: View3D: Away3D的入口类,即创建该类就会初始化一个可以使用GPU呈现3D的对象, ...

  8. Spring + iBatis 的多库横向切分简易解决思路

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  9. cocos2d-x 纹理深入研究

    转自:http://blog.csdn.net/qq51931373/article/details/9152227 1.纹理控制. 看此代码: CCSprite *pSprite = CCSprit ...

  10. <转>使用eclipse编译cocos2d-x示例项目,创建cocos2d-x android项目并部署到真机

    准备 今天将cocos2d-x的示例项目tests编译到android真机运行,以及如何创建cocos2d-x的android项目. 打开cocos2d-x的tests项目,路径为:D:\cocos2 ...