views:

46

answers:

2

Can someone explain me the way reusable cells works for single table view?

How many reusable cells a datasource should create? So far in all samples I've seen only one. Would one even need more?

+1  A: 

When creating a cell, you pass an identifier for that specific type of cell, so that later, you can try to get a preallocated one and only need to recreate one, if there is not a free one left (done automatically for you - just try to get one with the identifier as in the example code).

You can use as many different identifiers as you like, good practice is to use a different one for every different type of cell (which are probably of different UITableViewCell subclasses anyway).

So if you have CellTypeA and CellTypeB (both inheriting from UITableViewCell or setup very differently), use distinct keys for both types.

Eiko
Different means different subclasses of UITableViewCell or some different attributes of the cell? What makes cells "different"?
Michael
Different subclasses makes a different key mandatory - you couldn't configure a cell of wrong type anyway. If your configuring part (filling the cell with data and applying attributes to it) is heavyweight and different within one cell type, you might consider using different keys then, too, to prevent from doing this configuration over and over again. But in general, one key per cell type is a good starting point.
Eiko
+1  A: 

The datasource will be asked for one cell every time one is needed. You should therefore only create a new cell if none have previously been cached by the table.

Claus Broch
I understand that only 1 should be created. But even if I use different subclassed cells for the same datasource and styles also different, then shall I still use the same reuse identifier or have different identifier for each subclass/style and choose which identifier to dequeue?
Michael
The identifier only tells the tableview where to dequeue the cell when it has finished with it. It is completely up to you to determine what criteria you wish to use for grouping your cells with identifiers. As Eiko mentioned, a common practice is to use one identifier for each subclass.
Claus Broch