MVC模式:
M:就是Model(数据模型),保存应用程序的数据。
V:就是View(视图展示), 视图是可视化的,用户交互。
C:就是Controller(控制器),控制器实现逻辑操作,协调所有工作的中介者。
Model层:
Model.h
#import
@interface Model : NSObject
@property (nonatomic,copy)NSString *name;
@end
#import "Model.h"
@implementation Model
@end
View层:
MyCell.h
#import
#import "Model.h"
@interface MyCell :UITableViewCell
@property (nonatomic,strong)Model *model;
@end
MyCell.m
#import "MyCell.h"
@interface MyCell ()
@property (nonatomic,strong)UILabel *nameLabel;
@end
@implementation MyCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) {
[selfcreateCell];
}
returnself;
}
- (void)createCell{
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
[self.contentViewaddSubview:label];
self.nameLabel = label;
}
- (void)setModel:(Model *)model{
_model = model;
self.nameLabel.text = [NSStringstringWithFormat:@"名字:%@",model.name];
}
- (void)awakeFromNib {
[superawakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selectedanimated:animated];
}
Controller层:
#import "ViewController.h"
#import "MyCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfgetData];
}
- (void)getData{
self.dataArray = [NSMutableArrayarray];
Model *model = [[Modelalloc]init];
model.name =@"京津追梦人";
[self.dataArrayaddObject:model];
[self.myTableViewreloadData];
}
#pragma mark - UITableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
cell.model =self.dataArray[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return50;
}
- (UITableView *)myTableView{
if (_myTableView ==nil) {
UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];
tableView.delegate =self;
tableView.dataSource =self;
tableView.showsVerticalScrollIndicator =NO;
[tableView registerClass:[MyCellclass]forCellReuseIdentifier:@"cell"];
[self.viewaddSubview:tableView];
_myTableView = tableView;
}
return_myTableView;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
@end