1.xib中的结构:
2.代码:
#import "XMGStatusCell.h"
#import "XMGStatus.h"
@interface XMGStatusCell()
@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 *pictureView;
@end
@implementation XMGStatusCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"status";
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
return cell;
}
- (void)setStatus:(XMGStatus *)status
{
_status = status;
if (status.isVip) {
self.nameLabel.textColor = [UIColor orangeColor];
self.vipView.hidden = NO;
} else {
self.nameLabel.textColor = [UIColor blackColor];
self.vipView.hidden = YES;
}
self.nameLabel.text = status.name;
self.iconView.image = [UIImage imageNamed:status.icon];
if (status.picture) {
self.pictureView.hidden = NO;
self.pictureView.image = [UIImage imageNamed:status.picture];
} else {
self.pictureView.hidden = YES;
}
self.contentLabel.text = status.text;
}
- (CGFloat)height
{
if (self.pictureView.hidden) { // 没有配图
return CGRectGetMaxY(self.contentLabel.frame) + 10;
} else { // 有配图
return CGRectGetMaxY(self.pictureView.frame) + 10;
}
}
@end
#import "XMGStatusesViewController.h"
#import "XMGStatus.h"
#import "XMGStatusCell.h"
@interface XMGStatusesViewController ()
@property (strong, nonatomic) NSArray *statuses;
/** 存放所有cell的高度 */
@property (strong, nonatomic) NSMutableDictionary *heights;
@end
@implementation XMGStatusesViewController
- (NSArray *)statuses
{
if (_statuses == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *statusArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
XMGStatus *status = [XMGStatus statusWithDict:dict];
[statusArray addObject:status];
}
_statuses = statusArray;
}
return _statuses;
}
- (NSMutableDictionary *)heights
{
if (!_heights) {
_heights = [NSMutableDictionary dictionary];
}
return _heights;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGStatusCell *cell = [XMGStatusCell cellWithTableView:tableView];
cell.status = self.statuses[indexPath.row];
// 强制布局
[cell layoutIfNeeded];
// 存储高度
self.heights[@(indexPath.row)] = @(cell.height);
return cell;
}
#pragma mark - 代理方法
/**
* 返回每一行的高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// XMGStatusCell *cell = [XMGStatusCell cellWithTableView:tableView];
// cell.status = self.statuses[indexPath.row];
// // 强制布局
// [cell layoutIfNeeded];
// return cell.height;
//}
/**
* 返回每一行的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.heights[@(indexPath.row)] doubleValue];
}
/**
* 返回每一行的估计高度
* 只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:方法创建cell,再调用tableView:heightForRowAtIndexPath:方法获取cell的真实高度
*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
@end
3.效果