天天看點

storyboard自定義非等高的Cell

storyboard自定義非等高的Cell
storyboard自定義非等高的Cell
//
//  WeiboCell.m


#import "WeiboCell.h"
#import "WeiboModel.h"

@interface WeiboCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *vipView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *picView;

@end

@implementation WeiboCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    return [tableView dequeueReusableCellWithIdentifier:@"weibo"];
}

- (void)awakeFromNib
{

    //設定label每一行文字的最大寬度
    //讓UILabel計算尺寸更加準确
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;;
}

- (void)setModel:(WeiboModel *)model
{
    _model = model;
    
    if (model.isVip) {
        self.nameLabel.textColor = [UIColor orangeColor];
        self.vipView.hidden = NO;
    }else{
        self.nameLabel.textColor = [UIColor blackColor];
        self.vipView.hidden = YES;
    }
    
    self.iconView.image = [UIImage imageNamed:model.icon];
    self.nameLabel.text = model.name;
    self.contentLabel.text = model.text;
    if (model.picture) {
        self.picView.hidden = NO;
        self.picView.image = [UIImage imageNamed:model.picture];
    }else{
        self.picView.hidden = YES;
    }
    
    //強制布局
    [self layoutIfNeeded];
    
    //計算cell的高度
    if (self.picView.hidden) { //沒有配圖
        model.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10;
    }else{ //有配圖
        model.cellHeight = CGRectGetMaxY(self.picView.frame) + 10;
    }
}

@end