开发者

What's a Cell Identifier good for?

I wonder why I need a Cell Identifier in a UITableV开发者_运维问答iew... like this:

static NSString *cellIdentifier = @"Cell";

what's that needed for? Example?


A UITableView needs to be able to smoothly and quickly display rapidly-changing data, and sometimes cells have additional code to build the cell itself, using Core Graphics or the like.

A UITableView can only display a small number of cells at once, while there may be many more "rows" contained in the datasource. In order to reduce processing and memory usage, apple has provided the dequeueReusableCellWithIdentifier method. This allows the tableview to re-use an already instantiated cell that has dropped off the view, if available.

The UITableView can contain different types of cells. For example you might have a tableview where some rows had an associated image and others, did not, with different cell layouts. Or you have different cell types depending on some other upstream application setting. The CellIdentifier tells the dequeue method which type of cell you are looking to reuse, so you don't receive the wrong type of cell.

static NSString *CellIdentifier = @"Cell with image";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];

If a cell can't be found to reuse, then it must be created, ie:

if(cell == nil) ....

The confusion arises as many applications only use a single type of cell for a given tableview, so the CellIdentifier does not change.

Theres a good writeup from Apple at: https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html

See also: Use two different cell identifier on same table view

and: http://www.digitalhobbit.com/2009/12/19/a-useful-uitableview-cell-creation-pattern/


It's used as a key for caching of cells, for example:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * CellIdentifier    = @"MyCell1";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   ...

Then a different table might use a different identifier...


Cell Identifiers are good if you need to reuse cells to conserve application memory. For example, if you have many cells in your application, instead of releasing the cell once the user scrolls past it, the cell is simply modified to contain the info of new cells that you are creating. This conserves space since it isn't necessary to allocate thousands of cells if the user is only looking at 10 at a time. The identifier is what the system uses to check if there are cells with that identifier in existence already. If there are some, it uses them. Otherwise, it must allocate new space and create new objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜