I have a model class with 3 properties and tableview with three cells and each one has its own custom class.

How to install and fill three different cells from the same model correctly?

@interface Movie : NSObject
@property (nonatomic, strong) UIImage *posterImage;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *overview;
@end


@interface MoviePosterCell : UITableViewCell
@property (nonatomic, strong) UIImageView *poster;
@end

@interface MovieTitleCell : UITableViewCell
@property (nonatomic, strong) UILabel *title;
@end

@interface MovieOverviewCell : UITableViewCell
@property (nonatomic, strong) UILabel *overview;
@end

I tried to do something like this, but it seems to me wrong because if I want to reorder or add a new cell I will have to install everything manually.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        MovieOverviewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MovieOverviewCell class]) forIndexPath:indexPath];
        cell.poster = self.movie.posterImage;
        return cell;
    }

    if (indexPath.row == 1)
    {
        MovieTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MovieTitleCell class]) forIndexPath:indexPath];
        cell.title = self.movie.title;
        return cell;
    }

    if (indexPath.row == 2)
    {
        MovieOverviewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MovieOverviewCell class]) forIndexPath:indexPath];
        cell.overview = movie.overview;
        return cell;
    }

    return [UITableViewCell new];
}

0 Answers