Reusing UITableViewCells
来源:互联网 发布:javascript脚本怎么写 编辑:程序博客网 时间:2024/06/07 07:18
iOS devices have a limited amount of memory. If you were displaying a list with thousands of entries
in a UITableView, you would have thousands of instances of UITableViewCell. And your longsuffering iPhone would sputter and die. In its dying breath, it would say “You only needed enough cells
to fill the screen!”It would be right.
To preserve the lives of iOS devices everywhere, you can reuse table view cells. When the user scrolls
the table, some cells move offscreen. Offscreen cells are put into a pool of cells available for reuse.
Then, instead of creating a brand new cell for every request, the data source first checks the pool. If
there is an unused cell, the data source configures it with new data and returns it to the table view.
subclass UITableViewCellto create a special look or behavior. However, different subclasses floating
around the pool of reusable cells create the possibility of getting back a cell of the wrong type. Y ou
must be sure of the type of the cell returned to you so that you can be sure of what properties and
methods it has.
Note that you do not care about getting any specific cell out of the pool because you are going to
change the cell content anyway. What you need is a cell of a specific type. The good news is that every
cell has a reuseIdentifierproperty of type NSString. When a data source asks the table view for a
Chapter 8 UITableView and UITableViewController
174
reusable cell, it passes a string and says, “I need a cell with this reuse identifier.” By convention, the
reuse identifier is typically the name of the cell class.
In BNRItemsViewController.m, update tableView:cellForRowAtIndexPath:to reuse cells:
Previously, you created the table view cell explicitly, but now you are giving that control to Apple to
get the benefits of the reuse identifier. For this to work, you need to tell the table view which kind of
cell it should instantiate if there are no cells in the reuse pool.
In BNRItemsViewController.m, override viewDidLoadto register UITableViewCellclass with the
table view.
Reusing cells means that you only have to create a handful of cells, which puts fewer demands on
memory. Your application’s users (and their devices) will thank you. Build and run the application. The
behavior of the application should remain the same.
in a UITableView, you would have thousands of instances of UITableViewCell. And your longsuffering iPhone would sputter and die. In its dying breath, it would say “You only needed enough cells
to fill the screen!”It would be right.
To preserve the lives of iOS devices everywhere, you can reuse table view cells. When the user scrolls
the table, some cells move offscreen. Offscreen cells are put into a pool of cells available for reuse.
Then, instead of creating a brand new cell for every request, the data source first checks the pool. If
there is an unused cell, the data source configures it with new data and returns it to the table view.
Figure 8.12 Reusable instances of UITableViewCell
subclass UITableViewCellto create a special look or behavior. However, different subclasses floating
around the pool of reusable cells create the possibility of getting back a cell of the wrong type. Y ou
must be sure of the type of the cell returned to you so that you can be sure of what properties and
methods it has.
Note that you do not care about getting any specific cell out of the pool because you are going to
change the cell content anyway. What you need is a cell of a specific type. The good news is that every
cell has a reuseIdentifierproperty of type NSString. When a data source asks the table view for a
Chapter 8 UITableView and UITableViewController
174
reusable cell, it passes a string and says, “I need a cell with this reuse identifier.” By convention, the
reuse identifier is typically the name of the cell class.
In BNRItemsViewController.m, update tableView:cellForRowAtIndexPath:to reuse cells:
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"UITableViewCell"];// Get a new or recycled cellUITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"forIndexPath:indexPath];NSArray *items = [[BNRItemStore sharedStore] allItems];BNRItem *item = items[indexPath.row];cell.textLabel.text = [item description];return cell;}
Previously, you created the table view cell explicitly, but now you are giving that control to Apple to
get the benefits of the reuse identifier. For this to work, you need to tell the table view which kind of
cell it should instantiate if there are no cells in the reuse pool.
In BNRItemsViewController.m, override viewDidLoadto register UITableViewCellclass with the
table view.
- (void)viewDidLoad{[super viewDidLoad];[self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:@"UITableViewCell"];}
Reusing cells means that you only have to create a handful of cells, which puts fewer demands on
memory. Your application’s users (and their devices) will thank you. Build and run the application. The
behavior of the application should remain the same.
0 0
- Reusing UITableViewCells
- Reusing Classes
- Reusing Classes
- 第六章: Reusing Classes
- Chapter 6 Reusing Classes
- Stop MySQL Reusing AUTO_INCREMENT IDs
- java学习笔记6--Reusing classes
- Reusing Database Objects (in Jsp&Servlet)
- 重读Think In Java -- Reusing Class
- WARNING: You are creating/reusing datafile
- Reusing the implementation(复用具体实现)
- Think In Java 笔记3 Reusing Classes
- Reusing Code in C++: inheriting and template
- think in java3学习心得之——Reusing class
- Adobe Alchemy - Reusing C/C++ in Web application
- 处理教材:Reusing Classes of "Thinking in Java"
- Android Developers › AppWidgets: Reusing RemoteViews instance leaks memory?
- Make prettier documents by reusing chunks in RMarkdown
- 高师傅告诉的士大夫似的风格
- HIBERNATE的持久化对象加载策略
- Cracking coding interview(1.7) 设置某个位置为零的矩阵对应行列均为0
- Template Method - 模板方法模式
- Java--内部类的介绍
- Reusing UITableViewCells
- Powerdesigner显示列名
- HDU 2031(进制转换模板)
- C++IO系统学习
- POJ 3278--Catch That Cow
- memcache命令行
- 数据缓存与应用优化
- TIME_WAIT和CLOSE_WAIT(转)
- 高精度乘法