[自定義不等高的cell]-storyboard方式iOS8之后

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController


@end

// ViewController.m
#import "ViewController.h"
#import "XMGStatusCell.h"
#import "XMGStatus.h"
#import "MJExtension.h"

@interface ViewController ()
/** 所有的微博模型*/
@property (nonatomic ,strong) NSArray *statuses;
@end

@implementation ViewController


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


- (void)viewDidLoad {
    [super viewDidLoad];

    // iOS8之后
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.tableView.estimatedRowHeight =  44;

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 去緩存池里面找,如果沒有找到,去storyboard里面找
    static NSString *ID = @"status";
    XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 傳遞模型數據
    cell.status = self.statuses[indexPath.row];
    return cell;
}

@end

// 模型數據
// XMGStatus.h
#import <UIKit/UIKit.h>

@interface XMGStatus : NSObject

/** 圖像*/
@property (nonatomic ,copy) NSString *icon;

/** 昵稱*/
@property (nonatomic ,copy) NSString *name;

/** 內容(正文)*/
@property (nonatomic ,copy) NSString *text;

/** vip*/
@property (nonatomic ,assign ,getter=isVip) BOOL vip;

/** 配圖*/
@property (nonatomic ,copy) NSString *picture;

@end


// XMGStatus.m
#import "XMGStatus.h"

@implementation XMGStatus

@end

// XMGStatusCell.h
#import <UIKit/UIKit.h>

@class XMGStatus;
@interface XMGStatusCell : UITableViewCell

/** 微博模型*/
@property (nonatomic ,strong) XMGStatus *status;

@end

// XMGStatusCell.m
#import "XMGStatusCell.h"
#import "XMGStatus.h"


@interface XMGStatusCell ()
/** 圖像*/
@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;

/** 配圖的高度約束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureHLc;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottomLc;


@end

@implementation XMGStatusCell

// 設置數據
- (void)setStatus:(XMGStatus *)status
{
    _status = status;
    self.iconImageView.image = [UIImage imageNamed:status.icon];

    self.nameLabel.text = status.name;

    if (status.isVip) { // 是VIP
        self.vipImageView.hidden = NO;
        self.nameLabel.textColor = [UIColor orangeColor];
    } else {
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }

    self.text_Label.text = status.text;

    if (status.picture) { // 有配圖
        self.pictureHLc.constant = 100;
        self.pictureBottomLc.constant = 10;
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else {
        self.pictureHLc.constant = 0;
        self.pictureBottomLc.constant = 0;
    }

}
@end

約束圖片:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容