美文网首页
自定义tableViewCell,代码入口为-initWithS

自定义tableViewCell,代码入口为-initWithS

作者: Steven_Wu | 来源:发表于2019-02-22 10:53 被阅读0次

    在自定义 ELBloodRecordListCell时,习惯性的在-initWithFrame中,写了100多行布局子控件的代码,app运行后,居然使用的全是空白的系统cell,没有创建和添加任何子控件,经过排查,发现原因如下:

    自定义tableViewCell,代码入口为-initWithStyle:或者-awakeFromNib, 不能是-initWithFrame:

    //错误
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
        }
        return self;
    }
    
    
    //正确
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
        }
        return self;
    }
    

    但是UICollectionViewCell初始化时没有style参数的方法,故使用父类UIView通用的初始化方法-initWithFrame:

    @interface ELBloodRecordDetailCollectionCell : UICollectionViewCell
    
    @end
    
    
    @implementation ELBloodRecordDetailCollectionCell
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
        }
        return self;
    }
    
    

    相关文章

      网友评论

          本文标题:自定义tableViewCell,代码入口为-initWithS

          本文链接:https://www.haomeiwen.com/subject/yuvsyqtx.html