自定義等高的cell
等高的cell
所有cell的高度是一樣的
純代碼創(chuàng)建
frame
1,新建一個(gè)繼承自UITableViewCell的子類,比如XMGTgCell
@interfaceTgCell:UITableViewCell
@end
2,在TgCell.m文件中
重寫-initWithStyle:reuseIdentifier:方法
在這個(gè)方法中添加所有需要顯示的子控件
給子控件做一些初始化設(shè)置(設(shè)置字體、文字顏色等)
/**
* ?在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
? ? ? ? ? ? if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
? ? ? ? ? ? // ......
? ? ? ? ? ? ?}
?return ?self;
}
重寫-layoutSubviews方法
一定要調(diào)用[super layoutSubviews]
在這個(gè)方法中計(jì)算和設(shè)置所有子控件的frame
/**
* ?在這個(gè)方法中計(jì)算所有子控件的frame
*/
- (void)layoutSubviews{ ?
? ? ? ? ? ?[superlayoutSubviews];
? ? ? ? ? // ......
}
3,在TgCell.h文件中提供一個(gè)模型屬性,比如Tg模型
@classTg;
@interfaceTgCell:UITableViewCell/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property(nonatomic,strong) Tg *tg;
@end
4,在TgCell.m中重寫模型屬性的set方法
在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(Tg *)tg{ ?
? ? ? ? ? ? _tg = tg;
? ? ? ? ? ? // .......
}
5,在控制器中
注冊(cè)cell的類型
[self.tableViewregisterClass:[TgCell class] forCellReuseIdentifier:ID];
給cell傳遞模型數(shù)據(jù)
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? ? ? ? ? ? // 訪問緩存池
? ? ? ? ? ? ? TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
? ? ? ? ? ? ? cell.tg=self.tgs[indexPath.row];
? ? ? ? ? ? ? ?return cell;
}
XIB
新建一個(gè)繼承自UITableViewCell的子類,比如TgCell
@interfaceTgCell:UITableViewCell
@end
新建一個(gè)xib文件(文件名最好跟類名一致,比如TgCell.xib)
修改cell的class為TgCell
綁定循環(huán)利用標(biāo)識(shí)
添加子控件,設(shè)置子控件約束
將子控件連線到類擴(kuò)展中
@interfaceTgCell()
@property(weak,nonatomic)IBOutletUIImageView*iconImageView;
@property(weak,nonatomic)IBOutletUILabel*titleLabel;
@property(weak,nonatomic)IBOutletUILabel*priceLabel;
@property(weak,nonatomic)IBOutletUILabel*buyCountLabel;
@end
在TgCell.h文件中提供一個(gè)模型屬性,比如Tg模型
@classTg;
@interfaceTgCell:UITableViewCell/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property(nonatomic,strong) Tg *tg;
@end
在TgCell.m中重寫模型屬性的set方法
在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(Tg *)tg{ ?
? ? ? ? ?_tg = tg;?
? ? ? ? ?// .......
}
在控制器中
注冊(cè)xib文件
[self.tableViewregisterNib:[UINibnibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
給cell傳遞模型數(shù)據(jù)
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 訪問緩存池
? ? ? ? TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
? ? ? ? cell.tg=self.tgs[indexPath.row];
? ? ? ?returncell;
}
storyBoard
基本上與XIB一致,當(dāng)在緩存池中沒有找到數(shù)據(jù),會(huì)自動(dòng)去storyboard當(dāng)中去尋找,注意在要設(shè)置 identify否則在緩存池中會(huì)找不到這數(shù)據(jù)
不等高cell
純代碼創(chuàng)建
frame
一.給模型增加frame數(shù)據(jù)
所有子控件的frame
cell的高度
@interfaceStatus: NSObject ?
?/**** 文字\圖片數(shù)據(jù) ****/
// .....
/**** frame數(shù)據(jù) ****/
/** 頭像的frame */
@property(nonatomic, assign) CGRect iconFrame; ?
? // .....
/** cell的高度 */
@property(nonatomic, assign) CGFloat cellHeight;
@end
二.重寫模型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;
}
四. 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? ? ? ? ? ?staticNSString*ID =@"tg";
? ? ? ? ? ? ?// 訪問緩存池
? ? ? ? ? ? ?StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
? ? ? ? ? ? ?// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
? ? ? ? ? ? ?cell.status =self.statuses[indexPath.row];
? ? ? ? ? ? ?returncell;
}
五 新建一個(gè)繼承自UITableViewCell的子類,比如StatusCell
@interfaceStatusCell: UITableViewCell
@end
六 在StatusCell.m文件中
重寫-initWithStyle:reuseIdentifier:方法
? ? ? 在這個(gè)方法中添加所有需要顯示的子控件
? ? ? 給子控件做一些初始化設(shè)置(設(shè)置字體、文字顏色等)
/**
*? 在這個(gè)方法中添加所有的子控件
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
? ? ? ? ? ? ?if(self= [superinitWithStyle: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中重寫模型屬性的set方法- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setStatus:(Status*)status{?
? ? ? ? ? ? ?_status= status;
? ? ? ? ? ? // .......
}
九 重寫-layoutSubviews方法
一定要調(diào)用[super layoutSubviews]
在這個(gè)方法中設(shè)置所有子控件的frame
/**
*? 在這個(gè)方法中設(shè)置所有子控件的frame
*/
- (void)layoutSubviews{?
? ? ? ? ? ? [superlayoutSubviews];
? ? ? ? ? ? // ......
}
storyBoard創(chuàng)建
對(duì)比自定義等高cell,需要幾個(gè)額外的步驟(iOS8開始才支持)
添加子控件和contentView之間的間距約束
設(shè)置tableViewCell的真實(shí)行高和估算行高
// 告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來計(jì)算)
self.tableView.rowHeight = UITableViewAutomaticDimension; ? ?
?// 告訴tableView所有cell的估算高度
self.tableView.estimatedRowHeight =44;
根據(jù)文段最后底部結(jié)尾,可以利用autolayout的約束進(jìn)行控制,
設(shè)置與底部的長(zhǎng)度的約束,如果是圖片的話,要想不顯示圖片,可以利用約束控制這圖片的大小,注意如果是label的話,控制大小成線也會(huì)有些影響.
if(status.picture) { ?// 有配圖
self.pictureHeight.constant =100;
self.pictureBottom.constant =10;
self.pictureImageView.image = [UIImageimageNamed:status.picture];? ?
}else{ ? ?// 沒有配圖
self.pictureHeight.constant =0;// 設(shè)置圖片高度為0
self.pictureBottom.constant =0;// 設(shè)置圖片底部間距為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;
三在代理方法中計(jì)算cell的高度
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í)的尺寸)
? ? ? ? ? ?[selflayoutIfNeeded];
? ? ? ? ? ?// 計(jì)算cell的高度
? ? ? ? ? ?if(self.status.picture) {
? ? ? ? ? ? ? ? ? ? ? returnCGRectGetMaxY(self.pictureImageView.frame) +10;? ?
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ?return CGRectGetMaxY(self.text_label.frame) +10;? ?
? ? ? ? ? ?}
}