• 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
    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:

    在此之前调用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];
  • 相关阅读:
    luogu P4009 汽车加油行驶问题
    luogu P4015 运输问题
    luogu P2763 试题库问题
    luogu P4011 孤岛营救问题
    luogu P2765 魔术球问题
    linux 网卡
    linux yum错误
    ubuntu登录备注信息
    Ubuntu网卡配置
    linux 走三层内网添加静态路由
  • 原文地址:https://www.cnblogs.com/tinkl/p/3709500.html
Copyright © 2020-2023  润新知