ios-自定义tableViewcell以及注意点

tableViewCell 使用的区别, 
重写时候,  默认走initWithStyle:    如果想 自定义 cell 必须要通过 init 方法创建

(ps: 如果通过initWithStyle或者registerClass=====>不会走 init 进行初始化  )
//   cell 初始化方法
YYOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[YYOrderCell alloc] init];
    }
//自定义cell
#import "YYOrderCell.h"

@implementation YYOrderCell

- (instancetype)init{
    if (self = [super init]) {

        [self setupUI];// ========= 必须=======放在这个位置===
        NSLog(@"1");
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    NSLog(@"2");
    //[self setupUI];// ========== 如果=====放在这个位置==>无效
    return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
}

- (void)setupUI{
    
    UIImageView *pic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    pic.backgroundColor = [UIColor yellowColor];
    
    UITextView *viewtext = [[UITextView alloc] initWithFrame:CGRectMake(200, 10, 100, 100)];
    viewtext.backgroundColor = [UIColor grayColor];
    
    self.backgroundColor = [UIColor blueColor];
    self.contentView.backgroundColor = [UIColor redColor];
    
    // contentView 和 直接添加到cell的区别

    [self.contentView addSubview:pic];
    [self addSubview:viewtext];
    
    
}
@end

init和initWithStyle调用顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值