自定義等高的cell
等高的cell
所有cell的高度是一樣的
純代碼創建
frame
1,新建一個繼承自UITableViewCell的子類,比如XMGTgCell
@interfaceTgCell:UITableViewCell
@end
2,在TgCell.m文件中
重寫-initWithStyle:reuseIdentifier:方法
在這個方法中添加所有需要顯示的子控件
給子控件做一些初始化設置(設置字體、文字顏色等)
/**
* ?在這個方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
? ? ? ? ? ? if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
? ? ? ? ? ? // ......
? ? ? ? ? ? ?}
?return ?self;
}
重寫-layoutSubviews方法
一定要調用[super layoutSubviews]
在這個方法中計算和設置所有子控件的frame
/**
* ?在這個方法中計算所有子控件的frame
*/
- (void)layoutSubviews{ ?
? ? ? ? ? ?[superlayoutSubviews];
? ? ? ? ? // ......
}
3,在TgCell.h文件中提供一個模型屬性,比如Tg模型
@classTg;
@interfaceTgCell:UITableViewCell/** 團購模型數據 */
@property(nonatomic,strong) Tg *tg;
@end
4,在TgCell.m中重寫模型屬性的set方法
在set方法中給子控件設置模型數據
- (void)setTg:(Tg *)tg{ ?
? ? ? ? ? ? _tg = tg;
? ? ? ? ? ? // .......
}
5,在控制器中
注冊cell的類型
[self.tableViewregisterClass:[TgCell class] forCellReuseIdentifier:ID];
給cell傳遞模型數據
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? ? ? ? ? ? // 訪問緩存池
? ? ? ? ? ? ? TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設置數據(傳遞模型數據)
? ? ? ? ? ? ? cell.tg=self.tgs[indexPath.row];
? ? ? ? ? ? ? ?return cell;
}
XIB
新建一個繼承自UITableViewCell的子類,比如TgCell
@interfaceTgCell:UITableViewCell
@end
新建一個xib文件(文件名最好跟類名一致,比如TgCell.xib)
修改cell的class為TgCell
綁定循環利用標識
添加子控件,設置子控件約束
將子控件連線到類擴展中
@interfaceTgCell()
@property(weak,nonatomic)IBOutletUIImageView*iconImageView;
@property(weak,nonatomic)IBOutletUILabel*titleLabel;
@property(weak,nonatomic)IBOutletUILabel*priceLabel;
@property(weak,nonatomic)IBOutletUILabel*buyCountLabel;
@end
在TgCell.h文件中提供一個模型屬性,比如Tg模型
@classTg;
@interfaceTgCell:UITableViewCell/** 團購模型數據 */
@property(nonatomic,strong) Tg *tg;
@end
在TgCell.m中重寫模型屬性的set方法
在set方法中給子控件設置模型數據
- (void)setTg:(Tg *)tg{ ?
? ? ? ? ?_tg = tg;?
? ? ? ? ?// .......
}
在控制器中
注冊xib文件
[self.tableViewregisterNib:[UINibnibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
給cell傳遞模型數據
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 訪問緩存池
? ? ? ? TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 設置數據(傳遞模型數據)
? ? ? ? cell.tg=self.tgs[indexPath.row];
? ? ? ?returncell;
}
storyBoard
基本上與XIB一致,當在緩存池中沒有找到數據,會自動去storyboard當中去尋找,注意在要設置 identify否則在緩存池中會找不到這數據
不等高cell
純代碼創建
frame
一.給模型增加frame數據
所有子控件的frame
cell的高度
@interfaceStatus: NSObject ?
?/**** 文字\圖片數據 ****/
// .....
/**** frame數據 ****/
/** 頭像的frame */
@property(nonatomic, assign) CGRect iconFrame; ?
? // .....
/** cell的高度 */
@property(nonatomic, assign) CGFloat cellHeight;
@end
二.重寫模型cellHeight屬性的get方法
-(CGFloat)cellHeight{
? ? ? ? if(_cellHeight== 0){
? ? ? ? //...計算所有子控件的frame、cell的高度?
? ? ? ? }? ?
return ?_cellHeight;
}
三.在控制器中
實現一個返回cell高度的代理方法
在這個方法中返回indexPath位置對應cell的高度
/**
*? 返回每一行cell的具體高度
*/
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{?
? ? ? ? Status *status =self.statuses[indexPath.row];
? ? ? ? return ?status.cellHeight;
}
四. 給cell傳遞模型數據
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? ? ? ? ? ?staticNSString*ID =@"tg";
? ? ? ? ? ? ?// 訪問緩存池
? ? ? ? ? ? ?StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
? ? ? ? ? ? ?// 設置數據(傳遞模型數據)
? ? ? ? ? ? ?cell.status =self.statuses[indexPath.row];
? ? ? ? ? ? ?returncell;
}
五 新建一個繼承自UITableViewCell的子類,比如StatusCell
@interfaceStatusCell: UITableViewCell
@end
六 在StatusCell.m文件中
重寫-initWithStyle:reuseIdentifier:方法
? ? ? 在這個方法中添加所有需要顯示的子控件
? ? ? 給子控件做一些初始化設置(設置字體、文字顏色等)
/**
*? 在這個方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
? ? ? ? ? ? ?if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
? ? ? ? ? ? // ......
? ? ? ? ? ? ?}
return ?self;
}
七 在StatusCell.h文件中提供一個模型屬性,比如Tg模型
@class Status;
@interface StatusCell : UITableViewCell
/* 團購模型數據/
@property (nonatomic, strong) Status *status;
@end
- 八 在TgCell.m中重寫模型屬性的set方法- 在set方法中給子控件設置模型數據
- (void)setStatus:(Status*)status{?
? ? ? ? ? ? ?_status= status;
? ? ? ? ? ? // .......
}
九 重寫-layoutSubviews方法
一定要調用[super layoutSubviews]
在這個方法中設置所有子控件的frame
/**
*? 在這個方法中設置所有子控件的frame
*/
- (void)layoutSubviews{?
? ? ? ? ? ? [superlayoutSubviews];
? ? ? ? ? ? // ......
}
storyBoard創建
對比自定義等高cell,需要幾個額外的步驟(iOS8開始才支持)
添加子控件和contentView之間的間距約束
設置tableViewCell的真實行高和估算行高
// 告訴tableView所有cell的真實高度是自動計算(根據設置的約束來計算)
self.tableView.rowHeight = UITableViewAutomaticDimension; ? ?
?// 告訴tableView所有cell的估算高度
self.tableView.estimatedRowHeight =44;
根據文段最后底部結尾,可以利用autolayout的約束進行控制,
設置與底部的長度的約束,如果是圖片的話,要想不顯示圖片,可以利用約束控制這圖片的大小,注意如果是label的話,控制大小成線也會有些影響.
if(status.picture) { ?// 有配圖
self.pictureHeight.constant =100;
self.pictureBottom.constant =10;
self.pictureImageView.image = [UIImageimageNamed:status.picture];? ?
}else{ ? ?// 沒有配圖
self.pictureHeight.constant =0;// 設置圖片高度為0
self.pictureBottom.constant =0;// 設置圖片底部間距為0
}
如果要支持iOS8之前
一.如果cell內部有自動換行的label,需要設置preferredMaxLayoutWidth屬性
- (void)awakeFromNib{
? ? ? ? ? ? ? // 手動設置文字的最大寬度(目的是:讓label知道自己文字的最大寬度,進而能夠計算出自己的frame)
? ? ? ? ? ? ? ?self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width-20;
}
二.設置tableView的cell估算高度
// 告訴tableView所有cell的估算高度(設置了估算高度,就可以減少tableView:heightForRowAtIndexPath:方法的調用次數)
? ? ?self.tableView.estimatedRowHeight =200;
三在代理方法中計算cell的高度
StatusCell *cell;
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? ? ? ? // 創建一個cell(cell的作用:根據模型數據布局所有的子控件,進而計算出cell的高度)
? ? ? ? ? if(!cell) {? ? ?
? ? ? ? ? cell = [tableView dequeueReusableCellWithIdentifier:ID];?
? ? ? ? ? ? }
? ? ? ? ? ?// 設置模型數據
? ? ? ? ? ?cell.status =self.statuses[indexPath.row];
? ? ? ? ? ?return cell.height;
}
- (CGFloat)height{
? ? ? ? ? ?// 強制布局cell內部的所有子控件(label根據文字多少計算出自己最真實的尺寸)
? ? ? ? ? ?[selflayoutIfNeeded];
? ? ? ? ? ?// 計算cell的高度
? ? ? ? ? ?if(self.status.picture) {
? ? ? ? ? ? ? ? ? ? ? returnCGRectGetMaxY(self.pictureImageView.frame) +10;? ?
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?return CGRectGetMaxY(self.text_label.frame) +10;? ?
? ? ? ? ? ?}
}