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.

Figure 8.12  Reusable instances of UITableViewCell



There is one problem: sometimes a UITableViewhas different types of cells. Occasionally, you have to
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