纯代码cell

import

 //@class CHStatus; // 需要提供 模型数据 , 别人传 “微博”给我,我需要重写set 方法
  @class CHStatusesFrame; // 数据比 CHStatus 多

  @interface CHStatusesCell : UITableViewCell

  //@property (nonatomic, strong) CHStatus *statues; // strong 对象

  @property (nonatomic, strong) CHStatusesFrame *statusFrame;
 // 提供 一个接口
 // 把控制器tableview里的cell 封装了起来
  + (instancetype)cellWithTableView:(UITableView *)tableView;
  @end

在.m 文件里

  • 重写方法:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
      {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
          if (self) {
       
       // 1、添加 所有子控件
       // 头像
        UIImageView *iconIamge = [[UIImageView alloc] init];
        [self.contentView addSubview:iconIamge];
        self.iconView = iconIamge;
        
       // 昵称
        UILabel *nameView = [[UILabel alloc] init];
        nameView.font = kNameFont;
        [self.contentView addSubview:nameView];
        self.nameView = nameView;
        
       // 会员
        UIImageView *vipImage = [[UIImageView alloc] init];
        vipImage.image = [UIImage imageNamed:@"vip"]; // 性能优化,把它放在这里
        [self.contentView addSubview:vipImage];
        self.vipView = vipImage;
     }
}
  • 重写模型setter 方法
 - (void)setStatusFrame:(CHStatusesFrame *)statusFrame
      {
       // 在这个方法中设置“子控件的“ frame和显示数据
      _statusFrame = statusFrame;
        
      // 1 设置数据
      [self settingData];
      // 2 设置frame
      [self settFrame];
      }

      // 1 设置数据 -- 由于要调用多次,性能考虑
      - (void)settingData
      {
      // 微博数据(模型)
      CHStatus *status = self.statusFrame.status;
       // 头像
        self.iconView.image = [UIImage   imageNamed:status.icon];
       // 昵称
      self.nameView.text = status.name;
   
       // 配图 -- 经验(cell 里有配图和 没配图一定要判断)
        if (status.picture) {  // 有配图
        
        self.pictureView.hidden = NO;
        self.pictureView.image = [UIImage imageNamed:status.picture];
        } else {
    
        self.pictureView.hidden = YES;
          }
    
      }
      

2 设置frame

    - (void)settFrame
    {
    // 通常先设置微博数据
    CHStatus *status = self.statusFrame.status;
    // 1 头像
   
    self.iconView.frame = self.statusFrame.iconF;
   
    // 2 昵称
    self.nameView.frame = self.statusFrame.nameF;
    
    // 3 会员图标
    if (status.vip) {
        self.vipView.hidden = NO;
        self.nameView.textColor = [UIColor redColor];
    } else {
        self.vipView.hidden = YES;
        self.nameView.textColor = [UIColor blackColor];
    }
    // 4 正文
    self.textView.frame = self.statusFrame.textF;
    
    // 5 配图
   
    if (status.picture) { // 有配图,计算
        
        self.pictureView.hidden = NO;
        self.pictureView.image = [UIImage imageNamed:status.picture];
        
    } else {
    // 没有配图,则显示到正文
        self.pictureView.hidden = YES;
      }
    }

你可能感兴趣的:(纯代码cell)