先看看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. 常用的各种标准下载网站(HB GB GJB MH)

    标准分享网 http://www.bzfxw.com/ 标准下载网 http://www.bzxz.net/ 搜标准网   http://www.biaozhunw.com/Index.html 标准 ...

  2. [置顶] 解成电OJ1003真实的谎言的记录

    原题目 Description   N个人做一个游戏,游戏中每个人说了一句话(可能是真的也可能是假的) 第i个人说:“N个人中有至少有ai个,至多有bi个人说的是真话!”(i = 1, 2, 3….. ...

  3. SQLServer2005数据导入Mysql到详细教程

    如果转载请注明转载地址,谢谢. SQL SERVER数据导入MYSQL目录 1.Navicat for MySQL 版本10.0.9 2.创建目标数据库 3.创建正确的SQL SERVER数据库ODB ...

  4. centos6下安装部署hadoop2.2

    环境准备1.操作系统:centos6.0 64位2.hadoop版本:hahadoop-2.2.0 安装和配置步骤具体如下:1.主机和ip分配如下     ip地址                  ...

  5. 将某个组中的账户移动到新的OU下

    将某个组中的账户移动到新的OU下 #定义组名 $groupname = "testg" #定义新的OU名称 $newou = "OU=oo,OU=Admins,dc=dd ...

  6. UITableViewcell autolayout下动态高度

    项目中最经常使用的一个UI就是UITableView了.iOS7.8进一步优化了复用机制,用起来相当爽.配合Autolayout,适配工作减轻了非常多. 曾经做适配工作都是在heightForRow里 ...

  7. Linux下查看CPU型号,内存大小,硬盘空间命令

    1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重 ...

  8. node.js在windows下的学习笔记(8)---进程管理Process

    process是一个全局内置对象,可以在代码中的任何位置访问此对象,这个对象代表我们的node.js代码宿主的操作系统进程对象. 使用process对象可以截获进程的异常.退出等事件,也可以获取进程的 ...

  9. Mysql 5.5 replication 多数据库主从备份Master-Slave配置总结

    配置Mysql server 5.5 的双机备份,也就是master-slave模式.本例子还是一个多database复制的情况. 现在有两个database在同一台mysql server,也就是m ...

  10. java.util.Scanner的日常用法

    Scanner是新增的一个简易文本扫描器,在 JDK 5.0之前,是没有的.查看最新在线文档: public final class Scanner extends Object implements ...