Objective-C --- - UITableView 三 自定义cell(梳理总结)

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;

}

 

结果展示

 

 

 

转载于:https://my.oschina.net/adso/blog/691466

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值