码迷,mamicode.com
首页 > 移动开发 > 详细

ios registerNib: and registerClass:

时间:2014-05-08 20:17:35      阅读:752      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

先看看apple官网简述:

bubuko.com,布布扣
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
View Code

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:

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

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

 

registerClass:

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

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

 

  

具体实例如下:

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

bubuko.com,布布扣
#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];
}
bubuko.com,布布扣

RootCell.m:

bubuko.com,布布扣
//@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;
}
bubuko.com,布布扣

获取Cell对象:cellForItemAtIndexPath

bubuko.com,布布扣
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RootCell_ID forIndexPath:indexPath];
 
    return cell;
}
bubuko.com,布布扣

 

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

TODO:

bubuko.com,布布扣
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];
bubuko.com,布布扣

 

 

 

ios registerNib: and registerClass:,布布扣,bubuko.com

ios registerNib: and registerClass:

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/tinkl/p/3709500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!