ios registerNib: and registerClass:

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   tar   

先看看apple官網簡述:

registerNib:forCellWithReuseIdentifier:Register a nib file for use in creating new collection view cells.- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifierParametersnibThe 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.identifierThe reuse identifier to associate with the specified nib file. This parameter must not be nil and must not be an empty string.DiscussionPrior 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.AvailabilityAvailable in iOS 6.0 and later.See Also– registerClass:forCellWithReuseIdentifier:– dequeueReusableCellWithReuseIdentifier:forIndexPath:Declared InUICollectionView.hregisterNib: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 *)identifierParametersnibThe 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.kindThe 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.identifierThe reuse identifier to associate with the specified nib file. This parameter must not be nil and must not be an empty string.DiscussionPrior 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.AvailabilityAvailable in iOS 6.0 and later.See Also– registerClass:forSupplementaryViewOfKind:withReuseIdentifier:– dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:Declared InUICollectionView.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 - Utilsstatic 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];

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.