- (void)viewDidLoad {
[super viewDidLoad];
[self createTableView];
[self createDataSource];
}
-(void)createTableView
{
self.tableV=[[UITableViewalloc]initWithFrame:self.view.bounds style:0];
self.tableV.delegate=self;
self.tableV.dataSource=self;
[self.tableV registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//xib提前注册cell,name后面是xib的类名,cell是重用标识符
//新建的CustomCell必须继承于UitableViewCell
[self.view addSubview:self.tableV];
}
-(void)createDataSource
{
self.dataArr=[NSMutableArrayarray];
for (int i=0; i<20; i++) {
[self.dataArr addObject:@(i)];
}
}
-(CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
self.tableV.rowHeight=90;
return 90;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID=@"cell";
CustomCell* cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil) {
cell=[[NSBundlemainBundle]loadNibNamed:@"CustomCell" owner:self options:nil][0];
}
//不要在if判断外添加新的控件,会出现重影的情况(原因:tabelView对cell的重用机制)
cell.HeaderLabel.text=@"标题";
cell.DetailLabel.text=[NSString stringWithFormat:@"第%d行",indexPath.row];
cell.HeaderImage.image=[UIImage imageNamed:@"intro_3.jpg"];
/*
//contentView 是cell的属性用来显示内容的view
//[cell.contentView addSubview:label]
这句代码不用写,它用于非自定制cell的属性增加
*/
returncell;
}