這幾天在網(wǎng)上搜尋技術文章時,簡書上的文章總是很合胃口雹锣。于是就在簡書上面注冊了賬號,簡書上的文章都寫的很精髓癞蚕。這是我第一次在簡書上面寫文章蕊爵,多少有點小緊張,文章寫的哪里不好桦山,或者哪里有問題攒射,歡迎大家提出來,我會修正恒水。好了這里就不繼續(xù)瞎扯了会放,現(xiàn)在進入正題吧。
相信很多初級開發(fā)者們對于動態(tài)計算cell的行高都很頭大钉凌,總是計算不好咧最,導致各種問題。這里我就講一下平時我是怎樣計算行高的。有很多種方法矢沿,一種是通過約束來動態(tài)的計算行高滥搭,在《UITableView自動計算cell高度并緩存,再也不用管高度啦咨察!》這篇文章里寫的已經(jīng)很詳細了论熙,這里就不詳述了福青。還有一種是提前計算行高保存到模型中摄狱,這里先看一下效果圖;
這樣也實現(xiàn)了動態(tài)計算行高无午,每一個cell都返回不同的高度媒役。具體操作是先將數(shù)據(jù)模型傳入一個計算行高的模型當中,然保存這個模型到數(shù)據(jù)數(shù)組中宪迟。通過heightForRowAtIndexPath方法返回不同的行高酣衷。
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
WKTableViewCellFrame*cellFrame =self.dataArray[indexPath.row];
returncellFrame.rowHeight;
}
當然單單這樣籠統(tǒng)的說,相信大家也很難理解我在說什么次泽,別急聽我細細道來穿仪。
首先我們和往常一樣險些數(shù)據(jù)模型,有頭像意荤,昵稱啊片,文章里容。
#import
@interfaceWKFriend :NSObject
@property(copy,nonatomic)NSString*nickname;
@property(copy,nonatomic)NSString*content;
@property(copy,nonatomic)NSString*headImg;
@property(copy,nonatomic)NSString*picture;
+ (instancetype) friendInitWithDict:(NSDictionary*)dict;
@end
#import"WKFriend.h"
@implementationWKFriend
+ (instancetype)friendInitWithDict:(NSDictionary*)dict{
WKFriend*friend = [[WKFriendalloc]init];
[friendsetValuesForKeysWithDictionary:dict];
returnfriend;
}
- (void)setValue:(id)value forUndefinedKey:(NSString*)key{
}
@end
然后根據(jù)需求的布局玖像,提前進行cell的行高計算紫谷,根據(jù)控件數(shù)量添加模型屬性。
#import
#import"WKFriend.h"
@interface ? WKComputeCellFrame :NSObject
@property(assign,nonatomic) ?CGRect ?headImgFrame;
@property(assign,nonatomic) CGRect ? contentFrame;
@property(assign,nonatomic) CGRect ? nicknameFrame;
@property(assign,nonatomic) CGRect ? pictureFrame;
//rowHeight為計算出的cell行高
@property(assign,nonatomic) CGFloat ? rowHeight;
// friends是數(shù)據(jù)模型
@property(strong,nonatomic) WKFriend ?*friends;
@end
添加完成之后捐寥,要額外添加cell行高屬性和數(shù)據(jù)模型數(shù)據(jù)笤昨。
這里我們就可以開始布局了,這里提前計算沒個控件的frame握恳,然后進行布局瞒窒。
#import"WKComputeCellFrame.h"
@implementationWKComputeCellFrame
- (void)setFriends:(WKFriend*)friends{
_friends= friends;
self.headImgFrame = CGRectMake( 10, 20, 60, 60);
CGRect ?nickName = [friends.nickname boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:18]} context:nil];
self.nicknameFrame = CGRectMake( 10, CGRectGetMaxY(self.headImgFrame) + 5, nickName.size.width, nickName.size.height);
CGRect ?contentFrame = [friends.content boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:15]} context:nil];
self.contentFrame = CGRectMake( 10,CGRectGetMaxY(contentFrame), contentFrame.size.width,contentFrame.size.height);
if(friends.picture) {
//如果需要和qq空間一樣,文字下面顯示圖片就設置圖片的大小
self.pictureFrame=CGRectMake( 0, 0, 0, 0);
// 有圖片就是返回圖片的最大Y值
self.rowHeight=CGRectGetMaxY(self.pictureFrame) + 5;
return;
}
// 沒圖片就是返回文字的最大Y值
self.rowHeight=CGRectGetMaxY(self.contentFrame) + 5;
}
@end
其實我們自定義cell 的時候乡洼,就是文字內容的不確定性崇裁,導致我們無法靜精確的計算cell的行高。但是我們可以通過這個方法來計算文字的高度就珠。
[CGRect boundingRectWithSize:CGSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:18]} context:nil];
然后在我們寫數(shù)據(jù)數(shù)組時寇壳,將數(shù)據(jù)模型傳入計算模型當中,像這樣
for(NSDictionary*dictin_dataArray) {
WKFriend*friend = [WKFriend ?friendsInitWithDict:dict];
WKComputeCellFrame *cellFrame = [[WKTableViewCellFrame ?alloc]init];
cellFrame.friendMode = friends;
[mutableArray ?addObject:cellFrame];
}
傳入之后就可以返回行高了妻怎,在tableView的delegate的方法中返回不同的行高就行了壳炎。
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
WKTableViewCellFrame*cellFrame =self.dataArray[indexPath.row];
returncellFrame.rowHeight;
}
之后在自定義cell的時候,將我們提前計算好的frame賦值給控件就可以了。
-(void)setCellFrame:(WKTableViewCellFrame*)cellFrame{
_cellFrame= cellFrame;
_nickNameLable.frame= cellFrame.nickname;
_headImgView.frame= cellFrame.headImg;
_contentLab.frame= cellFrame.content;
self.nickNameLable.text = cellFrame.friends.nickname;
self.contentLab.text= cellFrame.friends.content;
self.imgView.image= [UIImageimageNamed:cellFrame.friends.headImg];
}
注意的是匿辩,Lable的文字大小要和提前計算好的大小一致
// 自定義cell中
UILabel*contentLab = [[UILabelalloc]init];
contentLab.font= [UIFontsystemFontOfSize:15];
contentLab.numberOfLines= 0;
// 計算模型中
CGRect ?contentFrame = [friends.content boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:15]} context:nil];
這樣我們就完成動態(tài)的返回cell的行高了腰耙。
各位讀者姥爺們,文章寫的比較匆忙铲球,哪里寫的不好希望大家多多指點挺庞。