如果在项目中要用到多种不同的tableViewCell,可以使用工厂模式。
主要思路是,Client 传递参数给工厂,工厂通过参数判断到底生产什么产品。主要利用面向对象的 多态的特性,父类指针指向子类对象。
废话不多说,一起看代码吧。
新建工程,新建一个RootTableViewController,一个BaseTableCell,一个BaseModel。新建三种cell,都继承于BaseTableCell。新建三种model,继承于BaseModel。创建完如下图:
将RootTableViewController作为window的根视图控制器
RootTableViewController *rootVC = [[RootTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navC;
- 根据数据填写model
// OneModel.h
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *imgsrc;
// TwoModel.h
@property (nonatomic, copy) NSString *imgsrc1;
@property (nonatomic, copy) NSString *imgsrc2;
@property (nonatomic, copy) NSString *imgsrc3;
// ThreeModel.h
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *picUrl;
@property (nonatomic, copy) NSString *singer;
- 根据model完成cell
// OneModelCell.h
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UILabel *titleLabel;
// TwoModelCell.h
@property (nonatomic, strong) UIImageView *imageV1;
@property (nonatomic, strong) UIImageView *imageV2;
@property (nonatomic, strong)