关键字
@class
用于声明,写在@interface之前,可以使用的类型
@interface
用于接口声明
使用格式:
1)在.h中
@interface。类名:继承的父类类名
@end
2)在.m中,写在@implementation之前
@interface 类名()
@end
*/
#import
@class Model;//声明此处使用Model类型
@interfaceTableViewCell :UITableViewCell
@property(nonatomic,strong)Model*model;
@end
#import "TableViewCell.h"
#import "Model.h"//引入,允许访问
@interface TableViewCell ()//接口
@property(nonatomic,strong)UIImageView*icon;//头像
@property(nonatomic,strong)UILabel*title;//标题
@property(nonatomic,strong)UILabel*subhead;//副标题
@property(nonatomic,strong)UIButton*play;//播放按钮
@property(nonatomic,strong)UIButton*more;//更多按钮
@end
/*
重写构造对象方法:
调用原来的构造方法
self=[super 构造方法];
if(self)
{
// 添加控件
}
return self
*/
//重写构造方法
@implementation TableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
// 调用父类
self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if(self)
{
[self addSubview:self.icon];
[self addSubview:self.title];
[self addSubview:self.subhead];
[self addSubview:self.play];
[self addSubview:self.more];
}
return self;
}
-(UIImageView *)icon
{
if(!_icon)
{
_icon=[[UIImageView alloc]init];
_icon.image=[UIImage imageNamed:@"EBFDDD9BFC9A303843C1B974EF4F58F6"];
}
return _icon;
}
//cell布局
- (void)layoutSubviews
{
[super layoutSubviews];
// 布局子控件
CGFloat width=self.frame.size.width;//宽
CGFloat height=self.frame.size.height;//高
CGFloatmoreW=30;
CGFloatmoreH=10;
CGFloatmoreY=(height-moreH)*0.5;
CGFloatmoreX=width-moreW-20;
self.more.frame=CGRectMake(moreX, moreY, moreW, moreH);
CGFloatplayW=20;
CGFloatplayH=15;
CGFloatplayY=(height-playH)*0.5;
CGFloatplayX=moreX-20-playW;
self.play.frame=CGRectMake(playX, playY, playW, playH);
CGFloattitleX=20;
CGFloattitleY=10;
CGFloattitleW=playX-20.0-20.0;
CGFloattitleH=25;
self.title.frame=CGRectMake(titleX, titleY, titleW, titleH);
CGFloatsubheadX=50;
CGFloatsubheadY=45;
CGFloatsubheadW=playX-20.0-20.0;
CGFloatsubheadH=20;
self.subhead.frame=CGRectMake(subheadX, subheadY, subheadW, subheadH);
CGFloaticonW=20;
CGFloaticonH=20;
CGFloaticonY=45;
CGFloaticonX=20;
self.icon.frame=CGRectMake(iconX, iconY, iconW, iconH);
}
-(UILabel*)title
{
if(!_title)
{
_title=[[UILabel alloc]init];
}
return _title;
}
-(UILabel*)subhead
{
if (!_subhead)
{
_subhead=[[UILabel alloc]init];
}
return _subhead;
}
-(UIButton*)play
{
if(!_play)
{
_play=[[UIButton alloc]init];
[_play setImage:[UIImage imageNamed:@"73D1DFC295DCADBBED5A23DCC3EA70C6"] forState:UIControlStateNormal];
}
return _play;
}
-(UIButton*)more
{
if(!_more)
{
_more=[[UIButton alloc]init];
[_more setImage:[UIImage imageNamed:@"11CE8B5E05E271F118B3552840516492"] forState:UIControlStateNormal];
}
return _more;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(void)setModel:(Model*)model
{
_model=model;
self.title.text=model.title;
self.subhead.text=model.subhead;
// self.play.isHidden=!model.isShowPlay;
if(model.isShowPlay==YES)
{
self.play.hidden=NO;//设置play按钮的隐藏属性为NO
}else
{
self.play.hidden=YES;
}
self.icon.hidden=!model.isShowIcon;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selectedanimated:animated];
// Configure the view for the selected state
}
@end
#import "ViewController.h"
#import "Model.h"
#import "TableViewCell.h"
@interface ViewController ()
@property(nonatomic,strong)UITableView*tableView;
@property(nonatomic,strong)NSMutableArray*dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
// Model*m0=[[Model alloc]init];
// m0.name=@"张三";
// m0.age=@"24";
// m0.title=@"断电";
// m0.subhead=@"霍建华/赵丽颖";
// m0.isShowPlay=YES;
// m0.isShowIcon=YES;
// [self.dataArray addObject:m0];
// Model*m1=[[Model alloc]init];
// m1.title=@"可不可以不勇敢";
// m1.subhead=@"范玮琪.真善美";
// m1.isShowPlay=YES;
// m1.isShowIcon=NO;
// [self.dataArray addObject:m1];
// Model*m2=[[Model alloc]init];
// m2.title=@"兰花指";
// m2.subhead=@"金则男.兰花指";
// m2.isShowPlay=YES;
// m2.isShowIcon=NO;
// [self.dataArray addObject:m2];
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"TableViewCellID"];
// 读取数据plist
// 1.获取文件路径
NSString*plistpath=[[NSBundle mainBundle]pathForResource:@"List" ofType:@"plist"];
// 2.获取数据。文件数据类型是数据
NSMutableArray*array=[[NSMutableArray alloc]initWithContentsOfFile:plistpath];
for(inti=0; i
{
NSDictionary*dict=array[i];
Model*m=[[Model alloc]init];
m.title=dict[@"title"];
m.subhead=dict[@"subhead"];
// m.isShowPlay=dict[@"isShowPlay"];
NSNumber*showPlay=dict[@"isShowPlay"];
BOOLisShowPlay=[showPlayboolValue];
m.isShowPlay=isShowPlay;
// m.isShowIcon=dict[@"isShowIcon"];
NSNumber*showIcon=dict[@"isShowIcon"];
BOOLisShowIcon=[showIconboolValue];
m.isShowIcon=isShowIcon;
[self.dataArray addObject:m];
}
// 3.遍历数组元素,构造模型对象,添加到dataArray
// for (NSDictionary*dict in array)
// {
//// for循环的for-in
// Model*model=[[Model alloc]init];
// model.title=dict[@"title"];
// model.subhead=dict[@"subhead"];
// model.isShowIcon=dict[@"isShowIcon"];
// model.isShowPlay=dict[@"isShowPlay"];
// [self.dataArray addObject:model];
// }
}
- (UITableView*)tableView
{
if (!_tableView)
{
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
}
return _tableView;
}
-(NSMutableArray *)dataArray
{
if (!_dataArray)
{
_dataArray=[NSMutableArray array];
}
return _dataArray;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cellID"];
// if (!cell)
// {
// cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
// }
// Model*m=self.dataArray[indexPath.row];
// cell.textLabel.text=[NSString stringWithFormat:@"姓名:%@,年龄:%@",m.name,m.age];
TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"TableViewCellID" forIndexPath:indexPath];
Model*m=self.dataArray[indexPath.row];
cell.model=m;
// cell.textLabel.text=[NSString stringWithFormat:@"姓名:%@,年龄:%@",m.name,m.age];
returncell;
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return70.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end