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

1.准备spa

 

 

2.实例化_tableView代理

    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];get

    [self.view addSubview:_tableView];it

    _tableView.delegate = self;io

    _tableView.dataSource = self;table

//    xib的注册方式class

//    [_tableView registerNib:[UINib nibWithNibName:kCellID bundle:nil] forCellReuseIdentifier:kCellID];List

//    纯代码的注册方式queue

    [_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;

}

 

结果展现

相关文章
相关标签/搜索