關于TableViewCell高度自適應問題的整理
來自: http://www.cnblogs.com/huketianxia/p/5174943.html
TableViewCell高度自適應在網上有很多資料,我只想找出最最最簡單的一種方法。
首先梳理一下思路。說到TableViewCell我們第一個想到的問題或許就是cell的復用問題。
1. [self.tableView registerClass:[Cell class] forCellReuseIdentifier:str]; 注冊之后可以在cell代理函數里調用
Cell *cell = [tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath]; 方法去對cell進行設置,操作,無需再進行復用處理。
2. 沒有進行register注冊的需要先判斷復用隊列里有沒有cell
1 Cell *cell = [tableView dequeueReusableCellWithIdentifier:str ]; 2 3 if (!cell) { 4 5 cell = [[Cell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:str]; 6 7 }
3.在 cellForRowAtIndexPath 函數里盡量不要去做添加控件的操作。因為,在if函數內添加,會出現數據重疊問題;而在if外面添加,會出現復用問題,黑乎乎一大片。如果確實需要,可以考慮自定義cell方法。分為純代碼和xib兩種方式,純代碼方式建立的是Class類,而xib方式創建的是nib類。
cell高度自適應
1.如果我們想要在cell里面新建lable并且使其自適應高度,那么只能新建Class類(其實是我沒找到可以用的nib解決方法)。初始化的時候添加控件,設置或者不設置其frame都可以,因為我們在下面會進行重新設置其frame。否則容易在斷行方面出問題
1 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 2 3 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 4 if (self) { 5 _lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, maiSrc.width, 44)]; 6 7 [self addSubview:_lable]; 8 9 _imgv = [[UIImageView alloc]init]; 10 11 [self addSubview:_imgv]; 12 13 } 14 return self; 15 }
2.在cellForRowAtIndexPath方法里不能直接對Cell對象進行賦值,那么我們可以建立對象方法來進行設置。當然,記得在.h文件里聲明這個對象方法。
1 #import "Cell.h" 2 3 static CGFloat heightForCell ; 4 static CGFloat widthForCell ; 5 6 @implementation Cell 7 8 -(void)setHeightForLable:(NSString *)str fontForLable:(CGFloat)font CGSize:(CGSize)constraintSize{ 9 10 //文字高度計算方法 11 CGFloat height = [[MineJN defaultMineJN] autoLayouHeightForLable:str fontSize:font constrainSize:CGSizeMake(maiSrc.width - widthForCell, CGFLOAT_MAX)]; 12 13 heightForCell = heightForCell > height ? heightForCell : height; 14 15 self.lable.text = str; 16 17 self.lable.numberOfLines = 0; 18 19 self.lable.frame = CGRectMake(self.lable.frame.origin.x, self.lable.frame.origin.y, maiSrc.width - widthForCell, heightForCell); 20 21 //獲取自身高度 然后將計算出來的高度賦值給他 22 CGRect frame = [self frame]; 23 24 frame.size.height = heightForCell; 25 26 self.frame = frame; 27 }
3.文字高度計算方法是我工具類里的方法
1 -(CGFloat)autoLayouHeightForLable:(NSString *)lableText fontSize:(CGFloat)fontSize constrainSize:(CGSize)maxSize{ 2 3 CGSize constraintSize; 4 5 //對比最大約束maxSize與CGSizeZero是否相等,如果是的話給他賦一個初值。以無限高為最大高度,以屏幕寬-30為寬度。如果不相等可以直接沿用 6 if (CGSizeEqualToSize(maxSize, CGSizeZero)) { 7 8 constraintSize = CGSizeMake(maiSrc.width, CGFLOAT_MAX); 9 }else{ 10 constraintSize = maxSize; 11 } 12 13 14 //計算方式 記得一般情況要有Origin 15 NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin; 16 /* 17 *字符串計算方式,NSStringDrawingUsesLineFragmentOrigin是以每行組成的矩形為單位計算整個文本的尺寸 18 *UsesFontLeading則以字體間的行距(leading,行距:從一行文字的底部到另一行文字底部的間距。)來計算。 19 *如果為NSStringDrawingTruncatesLastVisibleLine那么計算文本尺寸時將以每個字或字形為單位來計算。 20 *如果為NSStringDrawingUsesDeviceMetric,那么計算文本尺寸時將以每個字或字形為單位來計算。 21 */ 22 23 //設置字符串字體號,字體顏色 24 NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[[UIFont systemFontOfSize:fontSize],[UIColor blueColor]] forKeys:@[NSFontAttributeName,NSForegroundColorAttributeName]]; 25 26 //iOS7以后用這個方法來計算lable自定義高度 27 CGRect stringRect = [lableText boundingRectWithSize:constraintSize options:options attributes:dic context:nil]; 28 29 return stringRect.size.height; 30 }
4.寫完cell的對象方法,并且其中lable的各種屬性以及賦值情況都設置完成,那就可以到cellForRowAtIndexPath方法里去調用了
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 3 4 Cell *cell = [tableView dequeueReusableCellWithIdentifier:str]; 5 6 //獲取數據源 7 Info *io = _dataArr[indexPath.row]; 8 9 if (!cell) { 10 11 cell = [[Cell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:str]; 12 13 } 14 15 [cell setHeightForLable:io.intro fontForLable:18 CGSize:CGSizeMake(maiSrc.width, CGFLOAT_MAX)]; 16 17 return cell; 18 }
完了。這份代碼說的比較簡單,只是給出了一個思路,具體怎么實現,還需要你自己去動腦子思考,純粹的拿來主義很無聊,不管你在多久之后才看到這篇文章,都希望你能在遇到問題,看別人文章的時候進行認真思考。
</div>