The most important difference is, the forIndexPath:
version asserts (crashes) if you didn ' t register a class or NIB for the ID Entifier. The older (non- forIndexPath:
) version returns in the nil
.
You register a class for an identifier by sending to the registerClass:forCellReuseIdentifier:
table view. You register a nib-a identifier by sending to the registerNib:forCellReuseIdentifier:
table view.
If You create your table view and your cell prototypes in a storyboard, the storyboard loader takes care of registering th E cell prototypes that's defined in the storyboard.
Session 200-what ' s New in Cocoa Touch from WWDC to discusses the (then-new) forIndexPath:
version starting around 8m30s. It says that "you'll always get a initialized cell" (without mentioning that it'll crash if you didn ' t register a CLA SS or NIB).
The video also says that "it'll be is the right size for that index path". Presumably this means, it would set the cell's size before returning it, by looking at the table view ' s own width and C Alling your delegate ' s tableView:heightForRowAtIndexPath:
method (if defined). This is why it needs the index path.
Iphoneiosuitableview
There are two ways to reuse cells in UITableView:
iOS code
- -(ID) Dequeuereusablecellwithidentifier: (NSString *) identifier;
- -(ID) Dequeuereusablecellwithidentifier: (NSString *) identifier Forindexpath: (Nsindexpath *) Indexpath Ns_available_ IOS (6_0);
Dequeuereusablecellwithidentifier in iOS 6: replaced by Dequeuereusablecellwithidentifier:forindexpath:. As a result, creating and adding UITableViewCell objects in a tabular view becomes leaner and smoother. and using Dequeuereusablecellwithidentifier:forindexpath: will return to the cell, the system will automatically create a new cell when no cell can be reused by default.
Use Dequeuereusablecellwithidentifier:forindexpath: words, it must be used with the following two matching methods:
iOS code
- Beginning in IOS 6, clients can register a nib or class for each cell.
- If all reuse identifiers is registered, use the newer-dequeuereusablecellwithidentifier:forindexpath:to guarantee th At a cell instance is returned.
- Instances returned from the new Dequeue method would also be properly sized when they is returned.
- -(void) registernib: (uinib *) nib Forcellreuseidentifier: (NSString *) identifier Ns_available_ios (5_0);
- -(void) RegisterClass: (Class) Cellclass forcellreuseidentifier: (NSString *) identifier Ns_available_ios (6_0);
1. If a cell is customized with NIB, then call Registernib:forcellreuseidentifier:
2. If a cell is customized with code, then call Registerclass:forcellreuseidentifier:
These two methods can be called when creating a UITableView.
This allows you to omit the following code in the Tableview:cellforrowatindexpath: method:
iOS code
- static NSString *cellidentifier = @ "Cell";
- if (cell = = nil)
- cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];
Instead, it's the following code:
iOS code
- UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "cell" forindexpath:indexpath];
First, use NIB
1. The class of the specified cell in Xib is the type of the custom cell (not the class of file ' s owner)
2. Call Registernib:forcellreuseidentifier: Register the cell with the data source
iOS code
- [_tableview registernib:[uinib nibwithnibname:@ "Customcell" Bundle:nil] forcellreuseidentifier:kcellidentify];
3, in Tableview:cellforrowatindexpath: Use Dequeuereusablecellwithidentifier:forindexpath: To get the cell reuse, if there is no reuse of the cell, The cell is automatically created with the supplied nib file and returned (if you use Dequeuereusablecellwithidentifier: you need to determine if the return is empty)
iOS code
- Customcell *cell = [_tableview dequeuereusablecellwithidentifier:kcellidentify Forindexpath:indexpath];
4. If there is no reusable cell when getting the cell, a new cell is created and the Awakefromnib method is called
Second, do not use NIB
1. Rewrite the custom cell's Initwithstyle:withreuseablecellidentifier: Method for layout
2. Register cell
iOS code
- [_tableview Registerclass:[customcell class] forcellreuseidentifier:kcellidentify];
3, in Tableview:cellforrowatindexpath: Use Dequeuereusablecellwithidentifier:forindexpath: To get the cell reuse, if there is no reuse of the cell, The cell is automatically created with the class class provided and returned
iOS code
- Customcell *cell = [TableView dequeuereusablecellwithidentifier:kcellidentify Forindexpath:indexpath];
4. If there is no reusable cell when retrieving the cell, the Initwithstyle:withreuseablecellidentifier: method in cell will be called to create a new cell
Turn from:
http://stackoverflow.com/questions/25826383/ When-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi
http://eric-gao.iteye.com/blog/2234047
When to use Dequeuereusablecellwithidentifier vs Dequeuereusablecellwithidentifier:forindexpath