美文网首页
几种UITableview自定义Cell的办法

几种UITableview自定义Cell的办法

作者: 加菲猫哈哈哈哈 | 来源:发表于2018-07-23 10:03 被阅读9次

https://blog.csdn.net/feng2qing/article/details/51407156

1>纯代码自定义UITableviewCell

继承UITableviewCell,重写cell的initWithStyle:withReuseableCellIdentifier

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //布局cell内部控件        [self setupUI];

    }

    return self;

}

为tableView注册cell,使用registerClass:forCellReuseIdentifier:方法注册

[_tableView registerClass:[FooterCell Class] forCellReuseIdentifier:CellIdentifier];

在cellForRowAtIndexPath中使用dequeueReuseableCellWithIdentifier:forIndexPath:获取重用的cell,若无重用的cell,将自动使用所提供的class类创建cell并返回

FooterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

获取cell时若无可重用cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell

相关文章

网友评论

      本文标题:几种UITableview自定义Cell的办法

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