純代碼創(chuàng)建
frame
- 一.給模型增加frame數(shù)據(jù)
- 所有子控件的frame
- cell的高度
@interface Status : NSObject
/**** 文字\圖片數(shù)據(jù) ****/
// .....
/**** frame數(shù)據(jù) ****/
/** 頭像的frame */
@property (nonatomic, assign) CGRect iconFrame;
// .....
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
- 二.重寫(xiě)模型cellHeight屬性的get方法
- (CGFloat)cellHeight
{
if (_cellHeight == 0) {
// ... 計(jì)算所有子控件的frame、cell的高度
}
return _cellHeight;
}
- 三.在控制器中
- 實(shí)現(xiàn)一個(gè)返回cell高度的代理方法
- 在這個(gè)方法中返回indexPath位置對(duì)應(yīng)cell的高度
/**
* 返回每一行cell的具體高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Status *status = self.statuses[indexPath.row];
return status.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"tg";
// 訪(fǎng)問(wèn)緩存池
StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.status = self.statuses[indexPath.row];
return cell;
}
- 五 新建一個(gè)繼承自
UITableViewCell
的子類(lèi),比如StatusCell
@interface StatusCell : UITableViewCell
@end
- 六 在StatusCell.m文件中
- 重寫(xiě)
-initWithStyle:reuseIdentifier:
方法
- 在這個(gè)方法中添加所有需要顯示的子控件
- 給子控件做一些初始化設(shè)置(設(shè)置字體、文字顏色等)
/**
* 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// ......
}
return self;
}
- 七 在StatusCell.h文件中提供一個(gè)模型屬性,比如Tg模型
@class Status;
@interface StatusCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) Status *status;
@end
- 八 在TgCell.m中重寫(xiě)模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setStatus:(Status *)status
{
_status = status;
// .......
}
- 九 重寫(xiě)
-layoutSubviews
方法
- 一定要調(diào)用
[super layoutSubviews]
- 在這個(gè)方法中設(shè)置所有子控件的frame
/**
* 在這個(gè)方法中設(shè)置所有子控件的frame
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// ......
}
storyBoard創(chuàng)建
對(duì)比自定義等高cell,需要幾個(gè)額外的步驟(iOS8開(kāi)始才支持)
- 設(shè)置tableViewCell的真實(shí)行高和估算行高
// 告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來(lái)計(jì)算)
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告訴tableView所有cell的估算高度
self.tableView.estimatedRowHeight = 44;
- 根據(jù)文段最后底部結(jié)尾,可以利用autolayout的約束進(jìn)行控制,
設(shè)置與底部的長(zhǎng)度的約束,如果是圖片的話(huà),要想不顯示圖片,可以利用約束控制這圖片的大小,注意如果是label的話(huà),控制大小成線(xiàn)也會(huì)有些影響.
if (status.picture) { // 有配圖
self.pictureHeight.constant = 100;
self.pictureBottom.constant = 10;
self.pictureImageView.image = [UIImage imageNamed:status.picture];
} else { // 沒(méi)有配圖
// 設(shè)置圖片高度為0
self.pictureHeight.constant = 0;
// 設(shè)置圖片底部間距為0
self.pictureBottom.constant = 0;
如果要支持iOS8之前
- 一.如果cell內(nèi)部有自動(dòng)換行的label,需要設(shè)置preferredMaxLayoutWidth屬性
- (void)awakeFromNib
{
// 手動(dòng)設(shè)置文字的最大寬度(目的是:讓label知道自己文字的最大寬度,進(jìn)而能夠計(jì)算出自己的frame)
self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
- 二.設(shè)置tableView的cell估算高度
// 告訴tableView所有cell的估算高度(設(shè)置了估算高度,就可以減少tableView:heightForRowAtIndexPath:方法的調(diào)用次數(shù))
self.tableView.estimatedRowHeight = 200;
StatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 創(chuàng)建一個(gè)cell(cell的作用:根據(jù)模型數(shù)據(jù)布局所有的子控件,進(jìn)而計(jì)算出cell的高度)
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:ID];
}
// 設(shè)置模型數(shù)據(jù)
cell.status = self.statuses[indexPath.row];
return cell.height;
}
- (CGFloat)height
{
// 強(qiáng)制布局cell內(nèi)部的所有子控件(label根據(jù)文字多少計(jì)算出自己最真實(shí)的尺寸)
[self layoutIfNeeded];
// 計(jì)算cell的高度
if (self.status.picture) {
return CGRectGetMaxY(self.pictureImageView.frame) + 10;
} else {
return CGRectGetMaxY(self.text_label.frame) + 10;
}
}