UITableView 自定義單元格

myTableViewCell.m————————————————————

@property(nonatomic)UILabel* Lname;

@property(nonatomic)UILabel* LPrice;

@property(nonatomic)UILabel* LNameText;

@property(nonatomic)UILabel* LPriceText;

myTableViewCell.m————————————————————

- (void)awakeFromNib {

[superawakeFromNib];

// Initialization code

}

#

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[supersetSelected:selectedanimated:animated];

// Configure the view for the selected state

}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{

self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier];

if(self) {

UIColor* bgColor=[UIColorcolorWithRed:.07green:1.0blue:0.7alpha:1.0];//淡綠色

self.contentView.backgroundColor=bgColor;

//顯示書名的標簽

self.Lname=[[UILabelalloc]initWithFrame:CGRectMake(5,5,70,40)];

self.Lname.text=@"書名:";//設置該UILabel顯示的文本內容

self.Lname.textAlignment=NSTextAlignmentRight;//右對齊

self.Lname.font=[UIFontboldSystemFontOfSize:17];

self.Lname.backgroundColor=[UIColorclearColor];

//顯示書名值的標簽

self.LNameText=[[UILabelalloc]initWithFrame:CGRectMake(90,5,180,20)];

self.LNameText.textAlignment=NSTextAlignmentLeft;

self.LNameText.font=[UIFontboldSystemFontOfSize:18];

self.LNameText.textColor=[UIColorblueColor];

[self.contentViewaddSubview:self.LNameText];

//將標簽放入當前單元格中

[self.contentViewaddSubview:self.Lname];

self.LPrice=[[UILabelalloc]initWithFrame:CGRectMake(5,30,70,20)];

self.LPrice.text=@"價格:";

self.LPrice.textAlignment=NSTextAlignmentRight;

self.LPrice.font=[UIFontboldSystemFontOfSize:17];

self.LPrice.backgroundColor=[UIColorclearColor];

[self.contentViewaddSubview:self.LPrice];

self.LPriceText=[[UILabelalloc]initWithFrame:CGRectMake(90,30,180,20)];

self.LPriceText.textAlignment=NSTextAlignmentLeft;

self.LPriceText.textColor=[UIColorblueColor];

[self.contentViewaddSubview:self.LPriceText];

}

returnself;

}

ViewController.h——————————————————————

@property(nonatomic)NSArray* books;

@property(nonatomic)NSArray* prices;

@property(nonatomic)UITableView* tableView;

ViewController.m——————————————————————

- (void)viewDidLoad {

[superviewDidLoad];

//創(chuàng)建并初始化NSArray對象

self.books=[NSArrayarrayWithObjects:@"戰(zhàn)爭論",@"兵法",@"謀略",

@"全體戰(zhàn)",nil];

self.prices=[NSArrayarrayWithObjects:@"32.1",@"12",@"24",@"14",nil];

CGRectsize=[[UIScreenmainScreen]bounds];

self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,5, size.size.width, size.size.height)style:UITableViewStyleGrouped];

self.tableView.delegate=self;

self.tableView.dataSource=self;

[self.viewaddSubview:self.tableView];

// Do any additional setup after loading the view, typically from a nib.

}

//該方法的返回值決定各表格行的控件

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

//為表格行定義一個靜態(tài)字符串作為標識符

staticNSString* cellId=@"cellId";

//叢刻重用表格行的隊列中取出一個表格行

myTableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellId];

if(cell==nil) {

cell=[[myTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

}

NSUIntegerrowNo=indexPath.row;

cell.layer.cornerRadius=12;

cell.layer.masksToBounds=YES;

//為表格行的文本設置值

cell.LNameText.text=[self.booksobjectAtIndex:rowNo];

cell.LPriceText.text=[self.pricesobjectAtIndex:rowNo];

returncell;

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

return60;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

returnself.books.count;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容