先看看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];
