如何在一個cell重寫添加多個Label

首先運用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;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容