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];
//创建并初始化NSArray对象
self.books=[NSArrayarrayWithObjects:@"战争论",@"兵法",@"谋略",
@"全体战",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{
//为表格行定义一个静态字符串作为标识符
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.
}
网友评论