IOS 自定义cell(xib)

1:配置好cell并且选择其自定义类

Paste_Image.png

2:设置好cell的重用标识

Paste_Image.png

3:cell的创建

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"JQList";
    //1.判断是否存在可重用cell
    JQListCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        //2.为nib文件注册并指定可重用标识
        [tableView registerNib:[UINib nibWithNibName:@"JQListCell" bundle:nil] forCellReuseIdentifier:ID];
        
        //3.重新获取cell
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
    }
    //不要选中状态
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //4.返回cell
    return cell;
}
如果需要动态的调整cell的内容,那么需要在xib中关闭cell的自动布局

4:实现

- (void)awakeFromNib {
    [super awakeFromNib];
    //1.重新设置cell的宽度
    #warning 因为cell的默认宽度就是320,所以我们需要做这样一个修改
    self.width = JQMainScreenSize.width;
    
    //2.配置子控件
    self.nameLbl.backgroundColor=JQBlackColor;
    self.nameLbl.numberOfLines=0;
    self.nameLbl.font = JQFont;
    self.nameLbl.textColor=[UIColor whiteColor];
    
    self.explainsLbl.numberOfLines=0;
    self.explainsLbl.backgroundColor=JQRedColor;
    self.explainsLbl.textColor=[UIColor whiteColor];
    self.explainsLbl.font = [UIFont systemFontOfSize:14];
    
    //->nameLbl宽度固定,explainsLbl宽度伸缩
    self.explainsLbl.width = self.width - self.nameLbl.width;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容