Sunday, February 28, 2016

iOS Why user reuse Identifier with UITableView

User dequeueReusableCellWithIdentifier to speed things up. Instead of instantiating a lot of cells, you just instantiate as many as needed, i.e. as many that are visible (this is handled automatically). If scrolling to an area in the list where there are "cells" that haven't got their visual representation yet, instead of instantiating new ones, you reuse already existing ones.
        
        
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(@"new one");
}
else
{
     NSLog(@"old one");
}

If there are different kind of cells with different visual layout etc, create a new reuse identifier for that type of cell. This design follows the methodology of object pooling.
References:

No comments:

Post a Comment