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, 又称图形化界面) 是指采用图形方式显 ...
随机推荐
- 再次踩bug:遍历删除list(java.util.ConcurrentModificationException)
再次踩bug:遍历删除list(java.util.ConcurrentModificationException) 使用 List<Long> list = new ArrayList& ...
- [iOS基础控件 - 2] 按钮的基本使用
UIButton A.素材准备 1.图片素材放置到Images.xcassets中 B.按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNor ...
- CSS边框与边界
(上右下左依次体现)à边框的同一属性名称可以同时使用多个属性值 简化方案:border: 长度 形态 颜色 例如 border:1px solid black; 16.2 CSS中边界的使用 padd ...
- 用MyGeneration模板生成NHibernate映射文件和关系
用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many) MyGeneration的几个NHibernate模 ...
- Python FTP多线程爆破脚本
初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...
- HDU 5074 Hatsune Miku(DP)
Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and C ...
- paip.mysql备份慢的解决
paip.mysql备份慢的解决.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- net.ipv4.tcp_tw_recycle
原创 2016-03-07 CFC4N 运维帮 本文为翻译英文BLOG<Coping with the TCP TIME-WAIT state on busy Linux servers> ...
- iOS7 设置隐藏状态栏(status bar)
在info.plist 添加 UIViewControllerBasedStatusBarAppearance(View controller-based status bar appearance) ...
- Editing and Deleting Data
Editing and Deleting Data In the previous chapter we've come to learn how we can use the zend-form a ...