首先运用MVC模式,创建Model,View,Controller,
MyModel.h继承 NSObject
@property(nonatomic,strong)NSString *theMain;
@property(nonatomic,strong)NSString *theSecond;
@property(nonatomic,strong)NSString *theLast;
View放重写继承 UITableViewCell
NextTableViewCell.h
//图片 三个Label
@property(nonatomic,strong)UIImageView *image;
@property(nonatomic,strong)UILabel *theMainLabel;
@property(nonatomic,strong)UILabel *theSubLabel;
@property(nonatomic,strong)UILabel *theTagLable;
NextTableViewCell.m
//重写
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self addSubview:self.image];
[self addSubview:self.theMainLabel];
[self addSubview:self.theSubLabel];
[self addSubview:self.theTagLable];
}
return self;
}
//图片
- (UIImageView *)image
{
if (!_image)
{
_image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
_image.layer.masksToBounds = YES;
}
return _image;
}
//主Label
- (UILabel *)theMainLabel
{
if (!_theMainLabel)
{
_theMainLabel = [[UILabel alloc]initWithFrame:CGRectMake(100,0,200, 25)];
}
return _theMainLabel;
}
//次Label
- (UILabel *)theSubLabel
{
if (!_theSubLabel)
{
_theSubLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 25, self.frame.size.width, 25)];
}
return _theSubLabel;
}
//下Label
- (UILabel *)theTagLable
{
if (!_theTagLable)
{
_theTagLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, self.frame.size.width, 30)];
}
return _theTagLable;
}
// ViewController.m主界面
//导入
#import "NextTableViewCell.h"
#import "MyModel.h"
//协议
<UITableViewDelegate,UITableViewDataSource>
//全局变量
{
UITableView *_tableView;
MyModel *_model;
}
==============
// 添加标题
self.title = @"设置非MVC";
// 初始化表格
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
// 设置代理
_tableView.delegate = self;
_tableView.dataSource = self;
// 设置行高
_tableView.rowHeight = 80;
// 添加到视图上
[self.view addSubview:_tableView];
//==============
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@" "];
if (cell == nil)
{
cell = [[NextTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@" "];
}
_model = [[MyModel alloc]init];
if (indexPath.row == 0)
{
_model.theMain = @"iPhone开发秘籍";
_model.theSecond = @"85元";
_model.theLast = @"一本全方位介绍iPhone...";
cell.image.image = [UIImage imageNamed:@"1.jpg"];
cell.theMainLabel.text = _model.theMain;
cell.theSubLabel.text = _model.theSecond;
cell.theTagLable.text = _model.theLast;
}
else if (indexPath.row == 1)
{
_model.theMain = @"iPhone开发基础教程";
_model.theSecond = @"35元";
_model.theLast = @"一本iPhone开发的入门...";
cell.image.image = [UIImage imageNamed:@"2.jpg"];
cell.theMainLabel.text = _model.theMain;
cell.theSubLabel.text = _model.theSecond;
cell.theTagLable.text = _model.theLast;
}
else if (indexPath.row == 2)
{
_model.theMain = @"headerFirst设计模式";
_model.theSecond = @"69元";
_model.theLast = @"一本权威的介绍设计模...";
cell.image.image = [UIImage imageNamed:@"3.jpg"];
cell.theMainLabel.text = _model.theMain;
cell.theSubLabel.text = _model.theSecond;
cell.theTagLable.text = _model.theLast;
}
else if (indexPath.row == 3)
{
_model.theMain = @"javaScipt权威指南";
_model.theSecond = @"120元";
_model.theLast = @"一本js开发的百科全书";
cell.image.image = [UIImage imageNamed:@"4.jpg"];
cell.theMainLabel.text = _model.theMain;
cell.theSubLabel.text = _model.theSecond;
cell.theTagLable.text = _model.theLast;
}
return cell;
}
网友评论