Two ways to use a reusable cell

Source: Internet
Author: User

1. The most common method

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath **cell = [ TableView dequeuereusablecellwithidentifier:@ "cell"if (!  = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:@ "cell"    ]; }         return  cell;

2. Register for use

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath **cell = [ TableView dequeuereusablecellwithidentifier:@ "cell"  Forindexpath:indexpath];         return cell;}

Use this method to register when creating a TableView

// The function of this code is to tell TableView that the cell above is created from the UITableViewCell type. // If the reused cell cannot be found, the system will create a cell on its own basis, without the need for programmers to create it;  class] Forcellreuseidentifier:@ "cell"];

If you do not register first, the program will crash at run time.

Unable to dequeue a cell with identifier Cell-must register a nib or a class for the identifier or connect a prototype C Ell in a Storyboard

cannot be out of the team. A cell that is identified as cell, must register a nib or a class for this identifier or connect a prototype cell in storyboard

It seems to me that the first approach is more flexible and convenient.

1) The second method is supported in IOS 6 and later, for the earlier version is not supported, applied to the real machine, compatibility is a big problem;

2) The second method, although the code is more streamlined, but if you need to use a custom cell, that is, only to change the Textlabel on the cell is not enough to meet the requirements, the developer needs to add different view according to their own needs, this time, because not able to locate which cells are reused , which is not, add to get a cell all need to add view, so when the mobile phone screen, the re-use of the cell to add the same view again, with the increase in the number of slides, a cell overlay view more, resulting in a lot of memory consumption, which with cel L The idea of reuse runs counter to it;

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; Uiimageview*imageview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Image"]]; Imageview.frame= CGRectMake ( the,2, +, +);    [Cell Addsubview:imageview]; UILabel*label = [[UILabel alloc] Initwithframe:cgrectmake ( $,2, -, -)]; Label.text=@"Hello";    [Cell Addsubview:label]; UILabel*sublabel = [[UILabel alloc] Initwithframe:cgrectmake ( $, A, -, -)]; Sublabel.text=@"Hello";        [Cell Addsubview:sublabel]; returnCell;} //run the above code, and constantly swipe the screen, you will find that the font on the cell will be thick and black, this is because the stack of a lot of labels, the picture is superimposed a lot, the knowledge effect is not obvious, in the memory, you will find that memory consumption will be more and more large

3) The second method must be used before the TableView registration to use, or the program crashes, but in the development process is easy to forget, and use this method can not set the cell display style

When using the first method, if you want to add a view to the cell, the same part is done in the IF statement so that you do not overlay many of the same view on the same cell.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath{uitableviewcell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"];if( !cell) {Cell= [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"Cell"];//You can set the cell display style to SubTitleUiimageview *imageview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"Image"]]; Imageview.frame= CGRectMake ( the,2, +, +);        [Cell Addsubview:imageview]; UILabel*label = [[UILabel alloc] Initwithframe:cgrectmake ( $,2, -, -)]; Label.text=@"Hello";        [Cell Addsubview:label]; UILabel*sublabel = [[UILabel alloc] Initwithframe:cgrectmake ( $, A, -, -)]; Sublabel.text=@"Hello";    [Cell Addsubview:sublabel]; }returncell;}

Two ways to use a reusable cell

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.