自定义cell 其实就是 重建一个cell 继承UITableViewCell,然后再在tableView的方法里写。
先创建一个cell 类继承UITableViewCell,先看NewCell.h 文件。
#import <UIKit/UIKit.h>
@interface NewCell : UITableViewCell
@property (nonatomic,weak)UIImageView *iconView;
@property (nonatomic,weak)UILabel *labNameLabel;
@property (nonatomic,weak)UILabel *promptLabel;
+(instancetype)cellWithTabelView:(UITableView *)tableView;
@end
定义了几个属性 将是未来Cell 里面的内容,也定义了一个类方法,可以快速创建cell
再看下NewCell.m文件
#import "NewCell.h"
@implementation NewCell
#define NameFont [UIFont systemFontOfSize:15]
#define promptFont [UIFont systemFontOfSize:12];
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
+(instancetype)cellWithTabelView:(UITableView *)tabelView{
static NSString *ID = @"cell";
NewCell *cell = [tabelView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[NewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIImageView *iconView = [[UIImageView alloc]init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
<h3> </h3> UILabel *nameLabel = [[UILabel alloc]init];
nameLabel.font = NameFont;
[self.contentView addSubview:nameLabel];
self.labNameLabel = nameLabel;
UILabel *prompt = [[UILabel alloc]init];
prompt.font = promptFont;
prompt.textColor = [UIColor grayColor];
[self.contentView addSubview:prompt];
self.promptLabel = prompt;
[self.contentView setBackgroundColor:[UIColor clearColor]];
[self settingFrame];
}
return self;
}
-(void)settingFrame{
self.frame = CGRectMake(0, 0, 320, 80);
self.labNameLabel.frame = CGRectMake(100, 80/2-25, 200, 50);
self.iconView.frame =CGRectMake(10, (120-80)/2, 40, 40);
self.promptLabel.frame = CGRectMake(240, 80/2 -25, 200, 30);
}
@end
NewCell.m中定义了各个控件的位置,以及基本大小,颜色,等等。
再回到ViewController.m中 来创建一个表
#import "ViewController.h"
#import "NewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITableView *tableViewNew = [[UITableView alloc]initWithFrame:self.view.frame];
[self.view addSubview:tableViewNew];
tableViewNew.delegate = self;
tableViewNew.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat
{//调用 自定义的tableViewCell
NewCell *cell=[NewCell cellWithTabelView:tableView];
cell.labNameLabel.text=@"aaaa";
cell.iconView.image=[UIImage imageNamed:@"123.png"];
cell.promptLabel.text =@"bbbb";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.row);
}
完成几个必要的方法 。就基本完成了。