There are two ways to reuse cells in Ios-uitableview

Source: Internet
Author: User

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 That's 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 @" Cell " ;  

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil)  {    = [[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: 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
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

There are two ways to reuse cells in Ios-uitableview

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.