計算tableView不等高cell高度的幾種方法

2021-09-24 07:12:46 字數 2244 閱讀 2854

這裡利用heightforrowatindexpath:方法計算不等高cell的高度,在使用這個方法之前要明確這個方法的呼叫時間以及呼叫次數:

//返回cell的高度

- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

複製**

####計算不等高cell高度的第一種方法:估算高度

(void)viewdidload 

複製**

//定義間距

#define wylmargin 10

//返回cell的高度

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

context:nil].size.height;

cellheight += labelheight + wylmargin;

//底部導航欄的高度

cellheight += 35 + wylmargin;

nslog(@"heightforrowatindexpath-----%zd",indexpath.row);

return cellheight;

}複製**

####計算不等高cell高度的第二種方法:定義字典儲存cell高度

/** 儲存cell高度的字典*/

@property (nonatomic ,strong) nsmutabledictionary *cellheightdict;

/** 懶載入儲存cell高度的字典*/

-(nsmutabledictionary *)cellheightdict

return _cellheightdict;

}複製**

//返回cell的高度

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

context:nil].size.height;

cellheight += labelheight + wylmargin;

//底部導航欄的高度

cellheight += 35 + wylmargin;

//將計算過的cell高度儲存到字典中

self.cellheightdict[key] = @(cellheight);

nslog(@"heightforrowatindexpath----%zd",indexpath.row);

return cellheight;

}複製**

####計算不等高cell高度的第三種方法:在模型中定義cellheight屬性

#import 

@inte***ce

wyltopicitem : nsobject

/** 根據當前模型資料計算出來的cell高度*/

@property (nonatomic ,assign) nsinteger cellheight;

@end

複製**

#import "wyltopicitem.h"

@implementation

wyltopicitem

-(nsinteger)cellheight

context:nil].size.height;

_cellheight += labelheight + wylmargin;

//底部導航欄的高度

_cellheight += 35 + wylmargin;

return _cellheight;

}@end

複製**

-3.在heightforrowatindexpath:方法中計算cell的高度

//返回cell的高度

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

複製**

不等高列表布局

ff9 chrome15 opera11 safari5 win ie6 7 8 9 我們做某些場頻展示頁面的時候,一般是用列表來組織產品結構,比如 這種結構一般都是每一項的高度固定,但是如果有一項高度不固定的話,顯示就悲劇了。因為li的浮動就被前面那個高個兒截斷了,這一般也是浮動的悲哀。現在我們轉...

tableview非等高的cell

方法1 uitableviewcell tableview uitableview tableview cellforrowatindexpath nsindexpath indexpath cgfloat tableview uitableview tableview heightforrowat...

瀑布流布局(等寬不等高jQuery)

body div id main div class box div class pic img src picture 1.png div div div class box div class pic img src picture 2.png div div div class box div...