iOS Registernib:and registerclass:

Source: Internet
Author: User
Tags uikit

Take a look at Apple's website brief:

registerNib:forCellWithReuseIdentifier:Register a nib file forUseinchCreatingNewcollection view cells.- (void) registernib: (uinib *) nib Forcellwithreuseidentifier: (NSString *) identifierparametersnibthe nibObjectcontaining the cellObject. The nib file must contain only one top-levelObjectAnd thatObjectMust is of the type Uicollectionviewcell.identifierthe reuse identifier to associate with the specified nib file. This parameter must not being nil and must not being an emptystring. Discussionprior to calling the DequeueReusableCellWithReuseIdentifier:forIndexPath:method of the collection view, you mu St Use Thismethod or the RegisterClass:forCellWithReuseIdentifier:method to tell the collection view what to create aNewCell of the given type. If a cell of the specified type isNot currentlyinchA reuse queue, the collection view uses the provided information to create aNewCellObjectautomatically. If you previously registered aclassor nib file with the same reuse identifier, theObjectYou specifyinchThe nib parameter replaces the old entry. Specify nil forNibifYou want to unregister the nib file from the specified reuse identifier. AvailabilityavailableinchIos6.0and later. See also–registerclass:forcellwithreuseidentifier:–dequeuereusablecellwithreuseidentifier:forindexpath:declared InUICollectionView.hregisterNib:forSupplementaryViewOfKind:withReuseIdentifier:Registers a nib file forUseinchCreating supplementary views forThe collection view.- (void) registernib: (uinib *) nib Forsupplementaryviewofkind: (NSString *) kind withreuseidentifier: (NSString *) identifierparametersnibthe nibObjectcontaining the viewObject. The nib file must contain only one top-levelObjectAnd thatObjectMust is 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 stringMay correspond to one of the predefined kind strings or to a customstringThat the layout added to support aNewtype of supplementary view. This parameter must is nil.identifierthe reuse identifier to associate with the specified nib file. This parameter must not being nil and must not being an emptystring. Discussionprior to calling the DequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:method of the Collection view, you must use Thismethod or the RegisterClass:forSupplementaryViewOfKind:withReuseIdentifier:method to tell the collection view what to Crea Te a supplementary view of the given type. If A view of the specified type isNot currentlyinchA reuse queue, the collection view uses the provided information to create a viewObjectautomatically. If you previously registered aclassThe or nib file with the same element kind and reuse identifier, theclassYou specifyinchThe Viewclass parameter replaces the old entry. Specify nil forNibifYou want to unregister theclassFrom the specified element kind and reuse identifier. AvailabilityavailableinchIos6.0and 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:

Before this call Dequeuereusablecellwithreuseidentifier:forindexpath: The method of the collection view, You must use this method or tell the collection view how to create a new cell of the specified type by using the Registerclass:forcellwithreuseidentifier: method. If a cell of the specified type is not currently in the reuse queue, the collection view uses the information provided to automatically create a new cell object. If you previously registered a class or nib with the same identifier as the reuse, you replace the old entry with the object specified in the nib parameter. If you want to unregister the nib file from the specified reuse identifier, you can mark the nib as nil.

RegisterClass:

Before this call Dequeuereusablecellwithreuseidentifier:forindexpath: The method of the collection view, You must use this method or the Registernib:forcellwithreuseidentifier: method to tell the collection view how to create a new cell of the specified type. If a cell of the specified type is not currently in the reuse queue, the collection view uses the information provided to automatically create a new cell object. If you previously registered a reused class or nib file with the same identifier, the class that you specified in the Cellclass parameter supersedes the old entry. If you want to unregister a class from the specified reuse identifier, you can specify nilfor Cellclass.

  

The concrete examples are as follows:

MARK: Here is registernib in Viewdidload: ...

#pragma mark-view management-(void) viewdidload
{
// Build Collection View    _collectionview = [[Uicollectionview alloc] initWithFrame:self.view.bounds collectionviewlayout:layout];     = self ;    _collectionview. delegate = self ;     = [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;}

Get Cell object: Cellforitematindexpath

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath * ) indexpath{    *cell = [CollectionView dequeuereusablecellwithreuseidentifier:rootcell_id Forindexpath:indexpath];      return cell;}

It is also possible to handle registration events here at Cellforitematindexpath: But it is not recommended.

Todo:

StaticNSString *customcellidentifier =@"Customcellidentifier"; StaticBOOL nibsregistered =NO; if(!nibsregistered) {uinib*nib = [uinib nibwithnibname:@"Customcell"Bundle:nil];        [TableView registernib:nib Forcellreuseidentifier:customcellidentifier]; Nibsregistered=YES; } Customcell*cell = [TableView dequeuereusablecellwithidentifier:customcellidentifier];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.