iOS 重构:UITableViewCell

复用 cell

/// [源]自定义 UITableViewCell 获取方法(兼容OC)
static func dequeueReusableCell(_ tableView: UITableView, identifier: String, style: UITableViewCell.CellStyle = .default) -> Self {
   var cell = tableView.dequeueReusableCell(withIdentifier: identifier);
   if cell == nil {
        cell = self.init(style: style, reuseIdentifier: identifier);
   }

   cell!.selectionStyle = .none
   cell!.separatorInset = .zero
   cell!.layoutMargins = .zero
   cell!.backgroundColor = .white
   return cell as! Self;
}

/// [OC简洁方法]自定义 UITableViewCell 获取方法
static func dequeueReusableCell(_ tableView: UITableView) -> Self {
   return dequeueReusableCell(tableView, identifier: String(describing: self), style: .default)
}

Swift

//1.使用:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = UITableViewCellOne.dequeueReusableCell(tableView) 
     cell.labelLeft.text = String.init(format: "section_%d,row_%d", indexPath.section,indexPath.row);
     cell.labelRight.text = "990" + "\(indexPath.row)";
     cell.imgViewLeft.image = UIImage(named: "dragon.png");
     cell.imgViewRight.isHidden = false;
        
//     cell.getViewLayer();
     return cell;
}

//2. UITableViewCellOne 内部实现
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier);
       
    //图片+文字+文字+图片
    contentView.addSubview(imgViewLeft);
    contentView.addSubview(imgViewRight);
        
    contentView.addSubview(labelLeft);
    contentView.addSubview(labelRight);
}
    
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder);
    fatalError("init(coder:) has not been implemented")
}
    
override func layoutSubviews() {
    super.layoutSubviews();
    //布局自定义
}

OC

//思想:通过分类抽出一切通用的方法及参数
//1.使用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WHKTableViewOneCell *cell = [WHKTableViewOneCell dequeueReusableCell:tableView];
    
    cell.labelLeft.text = @"title";
    cell.imgViewRight.hidden = YES;//箭头
    
//    [cell  getViewLayer];
    return cell;
}

//2. WHKTableViewOneCell 内部实现
//导入类别:
#import "UITableViewCell+AddView.h"

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
        [self createControls];
        
    }
    return self;
}

- (void)createControls{
    //图片+文字+文字+图片
    [self.contentView addSubview:self.imgViewLeft];
    [self.contentView addSubview:self.imgViewRight];
    
    [self.contentView addSubview:self.labelLeft];
    [self.contentView addSubview:self.labelRight];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    // 布局自定义
    //图片+文字+文字+图片

}

UITableViewCell+AddView.h
UITableViewCellOne.swift

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容