IOS 自定义cell(手码)

JQCell向外提供的创建cell的方法

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"JQList";
    //1.判断是否存在可重用cell
    JQListCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        //2.为cell自定义类注册并指定重用标识
        [tableView registerClass:[JQListCell class] forCellReuseIdentifier:ID];
        cell = [[JQListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        
    }
    //不要选中状态
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //4.返回cell
    return cell;
}

JQCell内部重写该方法来添加子控件

/*
 *  重写该方法做初始化操作
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        
        #warning 因为一个cell的默认宽度就是320,所以我们需要做这样一个修改
        self.width = JQMainScreenSize.width;
        
        
        //1.创建子控件
        JQLabel *nameLbl = [[JQLabel alloc]init];
        JQLabel *explainsLbl = [[JQLabel alloc]init];
        nameLbl.backgroundColor=JQBlackColor;
        nameLbl.numberOfLines=0;
        nameLbl.font = JQFont;
        nameLbl.textColor=[UIColor whiteColor];
        
        explainsLbl.numberOfLines=0;
        explainsLbl.backgroundColor=JQRedColor;
        explainsLbl.textColor=[UIColor whiteColor];
        explainsLbl.font = [UIFont systemFontOfSize:14];
        
        [self.contentView addSubview:nameLbl];
        [self.contentView addSubview:explainsLbl];
        self.nameLbl = nameLbl;
        self.explainsLbl = explainsLbl;
        
        //2.设置子控件frame
        CGFloat nameX = 0;
        CGFloat nameY = 0;
        CGFloat nameW = 90;
        CGFloat nameH = 54;
        
        CGFloat explainsX = nameW;
        CGFloat explainsY = 0;
        CGFloat explainsW = self.width - nameW;
        CGFloat explainsH = nameH;
        nameLbl.frame = CGRectMake(nameX, nameY, nameW, nameH);
        explainsLbl.frame = CGRectMake(explainsX, explainsY, explainsW, explainsH);
    }
    return self;
}

你可能感兴趣的:(IOS 自定义cell(手码))