// 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;
}
NSString *ID = @"status";
- (void)viewDidLoad {
[super viewDidLoad];
// 注冊方法
[self.tableView registerClass:[XMGStatusCell class] forCellReuseIdentifier:ID];
// iOS8之后,界面排布
self.tableView.estimatedRowHeight = 200;
}
#pragma mark - 數據源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 傳遞模型數據
cell.status = self.statuses[indexPath.row];
return cell;
}
// 方案:在這個方法返回之前就要確定好cell的高度
XMGStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"");
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果沒有找到,那么就去注冊方法里面找
}
cell.status = self.statuses[indexPath.row];
return cell.cellHeight;
}
@end
// 模型數據
// XMGStatus.h
#import <UIKit/UIKit.h>
#define XMGTextFont [UIFont systemFontOfSize:14]
#define XMGNameFont [UIFont systemFontOfSize:17]
@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;
/** 圖像的frame*/
@property (nonatomic ,assign)CGRect iconFrame;
/** 昵稱的frame*/
@property (nonatomic ,assign)CGRect nameFrame;
/** vip的frame*/
@property (nonatomic ,assign)CGRect vipFrame;
/** 正文的frame*/
@property (nonatomic ,assign)CGRect textFrame;
/** 配圖的frame*/
@property (nonatomic ,assign)CGRect pictureFrame;
@property (nonatomic ,assign)CGFloat cellHeight;
@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;
/** cell的高度*/
- (CGFloat)cellHeight;
@end
// XMGStatusCell.m
#import "XMGStatusCell.h"
#import "XMGStatus.h"
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@interface XMGStatusCell ()
/** 圖像*/
@property (nonatomic ,weak)UIImageView *iconImageView;
/** 昵稱*/
@property (nonatomic ,weak)UILabel *nameLabel;
/** vip*/
@property (nonatomic ,weak)UIImageView *vipImageView;
/** 正文*/
@property (nonatomic ,weak)UILabel *text_Label;
/** 配圖*/
@property (nonatomic ,weak)UIImageView *pictureImageView;
@end
@implementation XMGStatusCell
// 把所有有可能顯示的子控件都先添加進去
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
CGFloat space = 10;
// 圖像
UIImageView *iconImageView = [[UIImageView alloc] init];
[self.contentView addSubview:iconImageView];
self.iconImageView = iconImageView;
// 添加約束
[iconImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.left).offset(space);
make.top.equalTo(self.contentView.top).offset(space);
make.width.equalTo(40);
make.height.equalTo(40);
}];
// 昵稱
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.font = XMGNameFont;
[self.contentView addSubview:nameLabel];
self.nameLabel = nameLabel;
// 添加約束
[nameLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(iconImageView.right).offset(space);
make.top.equalTo(iconImageView.top);
make.width.lessThanOrEqualTo(120);
}];
// vip
UIImageView *vipImageView = [[UIImageView alloc] init];
vipImageView.image = [UIImage imageNamed:@"vip"];
vipImageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:vipImageView];
self.vipImageView = vipImageView;
// 添加約束
[vipImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(nameLabel.right).offset(space);
make.top.equalTo(iconImageView.top);
make.width.equalTo(16);
make.height.equalTo(nameLabel.height);
}];
// 正文
UILabel *text_Label = [[UILabel alloc] init];
text_Label.font = XMGTextFont;
text_Label.numberOfLines = 0;
[self.contentView addSubview:text_Label];
// 手動設置label的文字的最大寬度(目的:為了能夠計算label的高度,得到最真實的尺寸)
text_Label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
self.text_Label = text_Label;
// 添加約束
[text_Label makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(iconImageView.left);
make.top.equalTo(iconImageView.bottom).offset(space);
make.right.equalTo(self.contentView).offset(-space);
}];
// 配圖
UIImageView *pictureImageView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureImageView];
self.pictureImageView = pictureImageView;
// 添加約束
[pictureImageView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(text_Label.bottom).offset(space);
make.left.equalTo(iconImageView.left);
make.width.equalTo(100);
make.height.equalTo(100);
}];
}
return self;
}
// 設置數據
- (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.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
// 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;
}
NSString *ID = @"status";
- (void)viewDidLoad {
[super viewDidLoad];
// 注冊方法
[self.tableView registerClass:[XMGStatusCell class] forCellReuseIdentifier:ID];
// iOS8之前,大概高度
self.tableView.estimatedRowHeight = 200;
}
#pragma mark - 數據源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 傳遞模型數據
cell.status = self.statuses[indexPath.row];
return cell;
}
// 方案:在這個方法返回之前就要確定好cell的高度
XMGStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果沒有找到,那么就去注冊方法里面找
}
cell.status = self.statuses[indexPath.row];
return cell.cellHeight;
}
@end
[自定義不等高的cell]-純代碼Autolayout方式iOS8之前
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- [自定義不等高的cell]-storyboard方式iOS8之前 筆者認真想了一天,怎么透徹的理解全局這個cell...
- 給模型增加frame數據 所有子控件的frame cell的高度 重寫模型cellH屬性的get方法 在控制器中 ...
- 新建一個繼承自UITableViewCell的子類,比如XMGTgCell 在XMGTgCell.m文件中 重寫-...
- 新建一個繼承自UITableViewCell的子類,比如XMGTgCell 在XMGTgCell.m文件中 重寫-...