自定義cell的高度

2021-07-05 12:02:58 字數 1426 閱讀 8823

步驟:

1.建立乙個繼承自uitableviewcell的類。

2.將cell中要用到的檢視都宣告成屬性

3.將這些檢視都新增到cell的contentview上

4.將資料物件也宣告成屬性,並重寫資料物件的setter方法,將該資料模型的屬性賦值給cell中的檢視上。

5.根據文字內容自定義cell的高度的主要**:

.h檔案中的**:

@inte***ce mycell : uitableviewcell

//cell要用的檢視

@property(nonatomic,strong) uilabel *namelabel;

@property(nonatomic,strong) uilabel *introducelabel;

//資料模型

@property(nonatomic,strong) student *student;

//根據模型計算出整個cell的高度

+(cgfloat)calcheightforcellwithstudent:(student *)student;

@end

.m檔案中的主要**:

@implementation mycell

//初始化方法

-(instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier

return  self;

}-(void)addallviews

//重寫setter方法(將資料模型的屬性賦值給cell的檢視)

-(void)setstudent:(student *)student

#pragma mark----------根據模型計算高度

//注意:傳乙個模型過來是方便以後更改

-(cgfloat)calcheightwithstudent:(student *)student

;//使用方法,去計算文字的frame

cgrect frame = [student.introduce boundingrectwithsize:size options:nsstringdrawinguseslinefragmentorigin attributes:dict context:nil];

//返回文字的高度

return frame.size.height;

}#pragma mark----------根據模型計算出整個cell的高度

+(cgfloat)calcheightforcellwithstudent:(student *)student

//注意:設定整個cell的高度的方法比設定整個cell的內容先執行,小心設定cell的高度時字典內容為空,程式會崩潰,是因為執行高度設定時檢視內沒有內容,這個問題在xib中容易出現。

自定義cell的高度

自定義cell的高度 1.把模型資料再次改寫成 乙個擁有frame和模型資料的模型類 2.在設定資料的frame類中把 擁有模型資料和frame的模型類 賦值 3.在 方法中heightforrow中只要拿到擁有frame和資料模型的模型類就可以 算出高度了 1.新建乙個繼承自uitableview...

自定義約束下的cell的高度

xib中包含了uilabel,由於uilabel的高度是不確定的,所以cell的高度也是不確定的 cgfloat tableview uitableview tableview heightforrowatindexpath nsindexpath indexpath ios7出現了估計高度 取代了...

xib自定義cell之自適應高度

第一種 計算高度 label多行顯示,計算size cgsize contentsize fxafactory fc getstringsizewith str boundingrectwithsize cgsizemake kscreen width 30 maxfloat font uifont...