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