標籤: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];