I am looking to return a number of custom UITableViewCells depending on the type of object I get back from my savedGames array stored in my dataModel. My question is, am I going about this the right way? I have not done this before and just wanted to make sure I was on the right track an not missing something obvious.
Note: new cells are alloc-ed and init-ed elsewhere, that is why that is not shown in the code below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
SharedDataModel *dataModel = [SharedDataModel sharedInstance];
id genericGameAtRow = [[dataModel savedGames] objectAtIndex:row];
if([genericGameAtRow isMemberOfClass:[GameZoneOne class]]) {
CellZoneOne *cell = [tableView dequeueReusableCellWithIdentifier:@"ZONEONE_ID"];
return cell;
}
if([genericGameAtRow isMemberOfClass:[GameZoneTwo class]]) {
CellZoneTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"ZONETWO_ID"];
return cell;
}
return nil;
}
EDIT: Quick question is returning nil at the bottom useful, I know it avoids the method not having a return value, but if none of the “if”s fire your going to get a “Must return a cell” error. Obviously its important to cover all the possible options, but would the final return not be better to return a default (vanilla) UITableViewCell instead? … just curious.
Yes this is a correct solution.
Personally I would stay away from
-[id<NSObject> isKindOfClass:], since it generally is a sign of bad architecture. Instead I would have added agameZoneproperty to all possible classes in thesavedGamesarray. And explicitly asked for it’s zone/type.