I have a genericTableViewController- GTBVC- class that goes into two other view controllers, each of which has its own data array, which is passed to this one GTBVC.
Currently my code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id object = [self.genericArray objectAtIndex:indexPath.row];
NSString *textNameOfObject = [[self.genericArray objectAtIndex:indexPath.row] performSelector:@selector(name)];
#warning - todo: Create a polymorphistic method for each uitableviewcells.
if([object isMemberOfClass:[OXPersonModel class]]){
OXFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellFriendIdentifier forIndexPath:indexPath];
cell.friendNameLabel.text = textNameOfObject;
[PKQuickMethods setCornerRadiusForView:cell.friendImageView withCornerRadius:CGRectGetWidth(cell.friendImageView.frame)/2];
cell.friendImageView.image = [UIImage imageNamed:@"image"];
[cell.friendImageView setContentMode:UIViewContentModeScaleToFill];
return cell;
}
else if([object isMemberOfClass:[OXTagPlaceCountModel class]]){
OXTagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellTagIdentifier forIndexPath:indexPath];
cell.tagNameLabel.text = textNameOfObject;
cell.tagPlacesCountLabel.text = [[self.genericArray objectAtIndex:indexPath.row] performSelector:@selector(totalPlacesCountString)];
return cell;
}
return nil;
}
So far this method is not so bad. However, since I want to make my code scalable for the future - when I expand the program to serve other cells and product types, I will need to ensure its easy implementation.
Polymorphism comes to mind. I thought that for each subclass of UITableView it is necessary to implement a common method name, for example -[cell updateForModelObject:], so that cellForRowAtIndexPaththere are only a few lines of code at the end .
, , . , , self.genericArray.
cellForRowAtIndexPath - if?