UITableViewCell Why the Static keyword is added when reusing IDs

Source: Internet
Author: User

UITableViewCell Why the Static keyword is added when reusing IDs

Take a look at iOS for various variable scopes and life cycle related knowledge:

1, the method of temporary variables stored in the stack, out of this method, the temporary variables will be automatically destroyed. However, if you add static to the temporary variable in the method, the stack does not reclaim the temporary variable until the program exits to release the memory, even if the method is out of the way.

2, string constants and global variables declared outside all methods, stored in the constant area, once generated will not be destroyed, until the program exits to free memory.

3, the object is generated and stored in the heap area, under the arc as long as there is no strong reference, the object's memory will be automatically recycled. Non-arc, when the reference count is 0 o'clock, the object memory is automatically reclaimed.

The following is the code that is commonly used when creating a cell

-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) indexpath{

static nsstring *id = @ " Hero_cell  ;

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier: ID];

if (!cell) {

cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier: ID];

}

}

Analysis:

If the static modifier is not added, it is written directly nsstring *id = @ "Hero_cell";

1, the string constant @ "Hero_cell", stored in the constant area, once generated, until the program exits to destroy;

2, the temporary variable ID has the stack area, the last curly brace} is automatically recycled by the stack, so that each time the Cellforrowatindexpath method is called, the temporary variable ID is regenerated in the stack, and it points to the constant zone @ "Hero_cell", consuming memory;

3, if added static, the variable ID in the stack will not be destroyed, has been pointing to the constant area of @ "Hero_cell", this is reasonable.

4, the diagram is as follows:

UITableViewCell Why the Static keyword is added when reusing IDs

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.