ios registerNib: and registerClass:
先看看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
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:的更多相关文章
- 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...
- iOS之UICollectionView详解
UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UIColle ...
- iOS Autolayout 在tableView scrollView 适用 学习
1 如何自动适应cell的高度 autolayout 里面 使用 systemLayoutSizeFittingSize 方法 (系统通过 已知的完整的Constraints和view的属性来计算 ...
- When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
The most important difference is that the forIndexPath: version asserts (crashes) if you didn't regi ...
- iOS---UICollectionView Class Reference---UICollectionView 类参考文档
UICollectionView 类: Inherits from UIScrollView : UIView : UIResponder : NSObject Conforms to NSCodin ...
- 为什么Tableviewcell创建时可以不判空
dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别: 前者不必向tableV ...
- uicollectionview registerclass vs registernib
当cell是用代码实现的时候用registerclass,当cell是用xib文件实现的时候用registernib
- iOS UITableView 引起的崩溃问题
其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...
- iOS学习32之UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
随机推荐
- fscanf(格式化字符串输入)
fscanf(格式化字符串输入) 相关函数 scanf,sscanf 表头文件 #include<stdio.h> 定义函数 int fscanf(FILE * stream ,const ...
- Integer的缓存机制
Java api 中为了提高效率,减少资源的浪费,对内部的Integer类进行了缓存的优化,通俗的说就是把-127至128这个范围内的数提前加载到内存,当我们需要的时候,如果正好在这个范围之内,就会直 ...
- 【Away3D代码解读】其它一些的记录(持续更新)
查看当前正在使用的AGAL代码可以在程序开始时添加下面的代码,AGAL代码会被trace出来: Debug.active = true; 具体的输出是在MaterialPassBase类的update ...
- 了解CSS/CSS3原生变量var
一.变量是个好东西 在任何语言中,变量的有一点作用都是一样的,那就是可以降低维护成本,附带还有更高性能,文件更高压缩率的好处. 随着CSS预编译工具Sass/Less/Stylus的关注和逐渐流行,C ...
- Android设计模式—策略模式
1.策略模式概念 定义一系列算法,把他们独立封装起来,并且这些算法之间可以相互替换.策略模式主要是管理一堆有共性的算法,客户端可以根据需要,很快切换这些算法,并且保持可扩展性. 策略模式的本质:分离算 ...
- 【转】rvm安装ruby,gem,rails,之后仍然无法找到rails命令
转自:http://chinacheng.iteye.com/blog/1738036 rvm安装ruby和rails之后,ruby -v好使,gem -v好使.但是rails -v不好使,提示没有安 ...
- Nginx动静分离经典
Nginx:安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等]把所需的包下载到/usr/src下[根据自己的习惯,路径可以改变]1.首先安装pcre: cd /usr/s ...
- 【源码分享】iOS-OC版五子棋
五子棋是程序猿比较熟悉的一款小游戏,相信很多人大学时期就用多种语言写过五子棋小游戏,笔者工作闲暇之余,试着用OC实现了一下,在这里给大家分享一下.有不足之处,欢迎大家提供建议和指点! GitHub源码 ...
- android EditText输入变化事件详解
editText.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) { // ...
- Java基础 Day14 泛型
//为什么要使用泛型 //1.解决元素存储的安全性的问题 //2.解决获取元素时,须要类型转换的问题 //未使用泛型 package org.tizen.test; import java.util. ...