When the cell becomes visible for the first time the init method will be used.
When the cell becomes visible not for the first time it will be dequeued from the memory of the table view.
UITableViewCell *cell = [searchTable dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
return cell;
Lets say I’ve scrolled the whole table and now any cell can be dequeued because they all have already been initialized.
My current cells have identifiers from 0 to 199. I refresh my table view and now I have new information for cells. I use method reloadData and use identifiers from 200 to 399 for new cells by adding +200 to cells identifier:
NSInteger index = indexPath.row + 200;
NSString *CellIdentifier = [NSString stringWithFormat:@"%d",index];
Now I scroll the whole table and see cells from 200 to 399.
Lets imagine I change index back to:
NSInteger index = indexPath.row;
Now a question: Old cells with identifiers from 0 to 199 can still be dequeued, can’t they?
If the answer is They CAN be dequeued I have another question:
Is there a way to remove cells with identifiers from 0 to 199 from table view memory when I start to use cells with identifiers from 200 to 399?
UITableViewdequeueReusableCellWithIdentifiermethod will handle that for you. It will deque only the visible cells and not all 200 cells if you are usingstatic [iTableView dequeueReusableCellWithIdentifier:cellIdentifier];Here is a discussion on this from apple discussion thread. Check that as well.
Update:
You need to modify your cell identifier. If you are creating new CellIdentifier for each row, there is no point in using
dequeueReusableCellWithIdentifiersince the identifier is different each and every time.Instead of
It should be,
That means each and every cell will be reused once the same is not visible. It will just pick cells which are not visible and will reuse it for your the next set of cells which are getting displayed. As per your implementation it will create 300 or 400 cells and you cant do much about removing previous ones since you no longer have any reference to the same.
Your method will look like this,
Update2: If you are not using ARC, it should be,
You need to have an
autoreleasefor the same.