Reputation: 61
I have a UITableView
that has different types of cells in it.The common approach is to have different UITableViewCell
s and reuse them depending on the data object.
Now, I have 9 different types of data objects. But my view is similar to the Facebook feed view with like button, comments button, user image and user name. Only the centre view changes based on the data object.
My question is, should I use 9 different type of cells with a common class for these elements or should I use one cell and add the centre view as and when the cell is created?
Currently my approach is to use one cell and add the centre view. Will the UITableViewCell
be re-used if we follow that approach?
Upvotes: 0
Views: 416
Reputation: 3954
The table view cells will always be reused if you initialize the cell with reuseIdentifier:
and use the dequeueReusableCellWithIdentifier:
method on the table view.
As far whether to use a single UITableViewCell
subclass or several, it depends on how much is different between each of your 9 content types. If they all contain the same UI elements, using 1 subclass makes sense. Otherwise, you can create multiple subclasses and still reuse the cells with dequeueReusableCellWithIdentifier:
as long as you pass in a different unique identifier for each subclass. Each subclass would be independently reused.
Here's what your cellForRowAtIndexPath:
could look like if you're using multiple cell classes:
NSString *primaryCellID = @"PrimaryCellID";
NSString *secondaryCellID = @"SecondaryCellID";
if (someCondition) {
CustomTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:primaryCellID];
if (!cell) {
cell = [[CustomTableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:primaryCellID];
}
return cell;
}
else {
CustomTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:secondaryCellID];
if (!cell) {
cell = [[CustomTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondaryCellID];
}
return cell;
}
Upvotes: 1