TableView circular references and two common bugs in a cache pool

Source: Internet
Author: User

First, directly see Bug:unable to dequeue a cell with identifier Cell_id-must register a nib or a class for the identifier or connect a Prototype cell in a storyboard

Second, the cell reuse mechanism using TableView does not display Detailtextlabel in the cell.

*

* The re-referencing mechanism of tableview in this paper

* When sliding up and down the tableview automatically destroys the Tableviewcell that are not displayed on the screen, and creates a new Tableviewcell to load new data while creating a new cell and destroying a cell that slides out of the screen. Consumes a lot of memory resources in the phone, so Apple quoted a new method after the 6.0 release, Dequeue reusable! The method name means: It can be re-referenced, the source file code is as follows:

*-(__kindof UITableViewCell *) Dequeuereusablecellwithidentifier: (NSString *) identifier Forindexpath: (NSIndexPath *) Indexpath Ns_available_ios (6_0); Newer Dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

* When the cell is newly created, the use of the secondary method will automatically find a match in the "cache pool", looking for the matching two requirements, 1. Unique identity ID, same 2.cell type

* When the cell slides out of the screen, the cell is automatically placed in the cache pool by the system, and a unique ID is bound and the type is declared when it is created.

* The unique identifier in the toolbar, such as

* Bugs appear in this place:

* Unable to dequeue a cell with identifier Cell_id-must register a nib or a class for the identifier or connect a proto Type cell in a storyboard

The meaning of this sentence is that the system looks for the cache pool with the same unique ID but the type is ambiguous, or the same type of xib is not found.

* Due to the search for a match to re-reference the cell in the "cache pool" to meet two conditions: The unique identity ID and unique identity ID corresponding to the same type, but Apple provides the second method can only determine the unique identity, so in the program when the Viewdidload to tell the system in the "Cache pool" The type that the unique ID corresponds to when looking for the cell.

* How to fix the bug:

* Register a cell with TableView and tell the System cell type style

* The code declared in Viewdidload is as follows:

* [Self.tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:id];

* 2. The second small problem

* Detailtextlabel is not displayed in cell after using the cell reuse mechanism of tableview.

* If you create a cell with the UITableViewCell class, the system default cell style is Uitableviewcellstyledefault, this style does not display Detaitextlabel, so to reconstruct a class, Override the Init method to set the cell format to Uitableviewcellstylesubtitle

* The cell style created by a class you wrote is Uitableviewcellstylesubtitle and can be displayed Detaitextlabel

* [Self.tableview Registerclass:[ertableviewcell class] forcellreuseidentifier:id];

* If you use a different style of cell, you can customize a class that inherits from UITableViewCell, and override the Init method to change the default style of the system when it is initialized

* Apple offers four types of cell: Uitableviewcellstyledefault (system default Style), UITableViewCellStyleValue1, UITableViewCellStyleValue2, Uitableviewcellstylesubtitle

* Source files are as follows

* Uitableviewcellstyledefault, //Simple cell with text label and optional image view (behavior of Uitableviewcel L in Iphoneos 2.x)

UITableViewCellStyleValue1,//left aligned label on left and right aligned label on right with blue text (used in Set Tings)

UITableViewCellStyleValue2, //Right aligned label on left with blue text and left aligned label on right (used in Pho ne/contacts)

Uitableviewcellstylesubtitle //left aligned label on top and left aligned label on bottom with gray text (used in IPo d).

* There are two notable points in a small case:

* 1.

The static modifier ID is used to keep the ID in the static zone, and the ID is not destroyed when not referenced, guaranteeing the stability of the address and value of the ID that uniquely identifies the ID at the time the program runs.

and @ "cell_id" is stored in the heap area

Static NSString * ID = @ "cell_id";

2.

The Weakalertcountroller is modified with __weak to make the copy into a strong reference after the block reference in the following, avoid circular references, and usually create a temporary object that is __weak decorated when the block references an external object in development. Calling a temporary object in a block

__weak typeof (Alertcountroller) Weakalertcountroller = Alertcountroller;

Source Code Portal: HTTP://PAN.BAIDU.COM/S/1GEXIYDL

*/

TableView circular references and two common bugs in a cache pool

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.