在iOS開發(fā)中葵萎,cell動(dòng)態(tài)行高的設(shè)置無疑很讓人抓狂囊颅,本文在此分享一個(gè)cell行高的動(dòng)態(tài)獲取方法;
首先笨腥,本文介紹的方法適用于直接new創(chuàng)建cell的情況翻诉,代碼如下:
cell樣式代理方法中添加:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//關(guān)鍵部分
CGRect rect = cell.frame;
//getCellHeight方法為自定義Cell中獲取布局高度的方法
rect.size.height = [cell getCellHeight];
cell.frame = rect;
}
注意次询,Label的高度自適應(yīng)設(shè)置捐晶,本文略過和屎,如要參考請移步鏈接:http://www.reibang.com/p/37a8414e015a
設(shè)置Cell的height窄坦,實(shí)現(xiàn)table的cell高度代理:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return 280;
}else if (indexPath.row == 1){
return 44;
}else{
//動(dòng)態(tài)計(jì)算行高 - wsx注釋
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];//獲取對象cell
return cell.frame.size.height;
}
}
這樣就能實(shí)現(xiàn)cell的動(dòng)態(tài)行高了钱烟;
荊軻刺秦王!