1.准备
2.实例化_tableView
_tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
// xib的注册方式
// [_tableView registerNib:[UINib nibWithNibName:kCellID bundle:nil] forCellReuseIdentifier:kCellID];
// 纯代码的注册方式
[_tableView registerClass:[MMTableViewCell class] forCellReuseIdentifier:MMCellID];
3.代理方法中 设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100.0f;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
这里没什么区别
// CustemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
MMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MMCellID];
// 在这里从数据源中取出数据 给到cell中
[cell getModel:self.dataList[indexPath.row]];
return cell;}
4.自定义的cell文件中
xib中,因为约束不好截图 只能给个大概
创建的时候会走的方法是 :
- (void)awakeFromNib { [super awakeFromNib];}
纯代码中
1.准备
#define KHeightOfCell 100
#define kTagOfDesc 1111
2.设置cell
//注册时候触发的方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle: style reuseIdentifier:reuseIdentifier]) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, KHeightOfCell - 20, KHeightOfCell - 20)];
imageView.image = [UIImage imageNamed:@"1"];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame), CGRectGetMinY(imageView.frame), 100, 20)];
title.text = @"标题";
UILabel *desc = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame), CGRectGetMaxY(imageView.frame) - 20, 100, 20)];
desc.text = @"描述";
desc.tag = kTagOfDesc;
[self.contentView addSubview:imageView];
[self.contentView addSubview:title];
[self.contentView addSubview:desc];
}
return self;
}
结果展示