xib自定义不等高cell(ios8之前)

2017-08-02  本文已影响13人  陆号

cell样式如下


cell样式.png

1.cell

@interface QPStatusCell ()

/** 图像 */
@property (nonatomic, weak)IBOutlet UIImageView *iconImageView;
/** 昵称 */
@property (nonatomic, weak)IBOutlet UILabel *nameLabel;
/** vip */
@property (nonatomic, weak)IBOutlet UIImageView *vipImageView;
/** 正文 */
@property (nonatomic, weak)IBOutlet UILabel *text_Label;
/** 配图 */
@property (nonatomic, weak)IBOutlet UIImageView *pictureImageView;


@end

@implementation QPStatusCell

- (void)awakeFromNib
{
    // 手动设置文字的最大宽度(让label能够计算出自己最真实的尺寸)
    self.text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}

/**
 *  设置子控件的数据
 */
- (void)setStatus:(XMGStatus *)status
{
    _status = status;
    self.iconImageView.image = [UIImage imageNamed:status.icon];
    self.nameLabel.text = status.name;
    
    if (status.isVip) {
        self.nameLabel.textColor = [UIColor orangeColor];
        self.vipImageView.hidden = NO;
    } else {
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    
    self.text_Label.text = status.text;
    
    if (status.picture) { // 有配图
        self.pictureImageView.hidden = NO;
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else { // 无配图
        self.pictureImageView.hidden = YES;
    }
}

- (CGFloat)cellHeight
{
    // 强制刷新(label根据约束自动计算它的宽度和高度)
    [self layoutIfNeeded];
    
    CGFloat cellHeight = 0;
    if (self.status.picture) { // 有配图
        cellHeight = CGRectGetMaxY(self.pictureImageView.frame) + 10;
    } else { // 无配图
        cellHeight = CGRectGetMaxY(self.text_Label.frame) + 10;
    }
    return cellHeight;
}
@end

2.UITableViewController

@interface ViewController ()

/** 所有的微博数据 */
@property (nonatomic, strong) NSArray *statuses;
@end

@implementation ViewController

- (NSArray *)statuses
{
    if (!_statuses) {
        _statuses = [QPStatus mj_objectArrayWithFilename:@"statuses.plist"];
    }
    return _statuses;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置估算高度 (减少tableView:heightForRowAtIndexPath:的调用次数)
    self.tableView.estimatedRowHeight = 200;
}

NSString *ID = @"status";

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 访问缓存池
    QPStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 传递模型数据
    cell.status = self.statuses[indexPath.row];
    return cell;
}

QPStatusCell *cell;
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建一个临时的cell(目的:传递indexPath对应这行的模型,布局内部所有的子控件,得到cell的高度)
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
    }
    // 传递模型数据
    cell.status = self.statuses[indexPath.row];
    
    return cell.cellHeight;
}
@end
上一篇下一篇

猜你喜欢

热点阅读