2013-12-12 38 views
0

從我的理解中,將UITableViewControllers中的可重用單元出列的主要好處是通過僅將當前可見單元及其內容保存在內存中來節省內存。正確地重複使用UITableViewCells和自定義高度

這就是說,它似乎是有道理的每一個細胞被請求的cellForRowAtIndexPath內(分別爲GetCell()在MonoTouch的),並釋放了整個小區時不使用/可見的再一次創建一個單元格的內容。

如果所有單元具有相同的高度,則工作正常。但是,如果只有一個單元格具有另一個動態高度(通過創建內容視圖計算),則高度必須設置在heightForRowAtIndexPath(分別在MonoTouch中爲GetHeightForRow())。

現在我感到困惑,因爲heightForRowAtIndexPath似乎要調用表中的所有單元格,即使當前不在viewport中的單元格也是如此。

請參閱下面的日誌(第一個數字是部分指標,二是行索引):

2013-12-12 13:00:29.322 MyApp[32603:80b] GetHeightForRow - 1 - 0 
2013-12-12 13:00:29.323 MyApp[32603:80b] GetHeightForRow - 1 - 1 
2013-12-12 13:00:29.323 MyApp[32603:80b] GetHeightForRow - 1 - 2 
2013-12-12 13:00:29.323 MyApp[32603:80b] GetHeightForRow - 1 - 3 
2013-12-12 13:00:29.324 MyApp[32603:80b] GetHeightForRow - 1 - 4 
2013-12-12 13:00:29.324 MyApp[32603:80b] GetHeightForRow - 1 - 5 
2013-12-12 13:00:29.325 MyApp[32603:80b] GetHeightForRow - 1 - 6 
2013-12-12 13:00:29.325 MyApp[32603:80b] GetHeightForRow - 1 - 7 
2013-12-12 13:00:29.326 MyApp[32603:80b] GetHeightForRow - 1 - 8 
2013-12-12 13:00:29.326 MyApp[32603:80b] GetHeightForRow - 1 - 9 
2013-12-12 13:00:29.326 MyApp[32603:80b] GetHeightForRow - 1 - 10 
2013-12-12 13:00:29.327 MyApp[32603:80b] GetHeightForRow - 1 - 11 
2013-12-12 13:00:29.327 MyApp[32603:80b] GetHeightForRow - 1 - 12 
2013-12-12 13:00:29.328 MyApp[32603:80b] GetHeightForRow - 1 - 13 
2013-12-12 13:00:29.328 MyApp[32603:80b] GetHeightForRow - 1 - 14 
2013-12-12 13:00:29.329 MyApp[32603:80b] GetHeightForRow - 1 - 15 
2013-12-12 13:00:29.329 MyApp[32603:80b] GetHeightForRow - 1 - 16 
2013-12-12 13:00:29.330 MyApp[32603:80b] GetHeightForRow - 1 - 17 
2013-12-12 13:00:29.330 MyApp[32603:80b] GetHeightForRow - 1 - 18 
2013-12-12 13:00:29.330 MyApp[32603:80b] GetHeightForRow - 1 - 19 
2013-12-12 13:00:29.331 MyApp[32603:80b] GetHeightForRow - 1 - 20 
2013-12-12 13:00:29.331 MyApp[32603:80b] GetHeightForRow - 1 - 21 
2013-12-12 13:00:29.331 MyApp[32603:80b] GetHeightForRow - 1 - 22 
2013-12-12 13:00:29.332 MyApp[32603:80b] GetHeightForRow - 1 - 23 
2013-12-12 13:00:29.332 MyApp[32603:80b] GetHeightForRow - 1 - 24 
2013-12-12 13:00:29.332 MyApp[32603:80b] GetHeightForRow - 0 - 0 
2013-12-12 13:00:29.333 MyApp[32603:80b] GetHeightForRow - 0 - 1 
2013-12-12 13:00:29.335 MyApp[32603:80b] GetCell - 0 - 0 
2013-12-12 13:00:29.344 MyApp[32603:80b] GetCell - 0 - 1 
2013-12-12 13:00:29.347 MyApp[32603:80b] GetCell - 1 - 0 
2013-12-12 13:00:29.349 MyApp[32603:80b] GetCell - 1 - 1 
2013-12-12 13:00:29.351 MyApp[32603:80b] GetCell - 1 - 2 
2013-12-12 13:00:29.354 MyApp[32603:80b] GetCell - 1 - 3 
2013-12-12 13:00:29.356 MyApp[32603:80b] GetCell - 1 - 4 
2013-12-12 13:00:29.358 MyApp[32603:80b] GetCell - 1 - 5 
2013-12-12 13:00:29.361 MyApp[32603:80b] GetCell - 1 - 6 
2013-12-12 13:00:29.363 MyApp[32603:80b] GetCell - 1 - 7 

有沒有辦法來渲染表之前沒有創建所有內容的意見,這是我只是看不到?或者,我的基本假設是錯誤的,那麼這將是關於性能的最佳方式,而不是之前創建所有內容視圖?

由於

+1

我建議預先計算單元的高度並將它們存儲在一個數組中。 – duci9y

+0

如果我願意,我還需要在顯示錶格之前創建所有內容視圖,因此預期的性能優勢將消失。 –

+0

不一定。如果是文本,則可以非常輕鬆高效地計算大小。 – duci9y

回答

0

基本上TableViews正常工作是這樣的:當需要時

細胞被創建,即,僅創建顯示在屏幕上的細胞數。這是因爲分配和啓動對象很昂貴。然後,當用戶滾動時,單元格從表格視圖中出列,並且新內容被添加到該單元格。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath針對表格視圖中的每一行調用,而不管是否顯示單元格。 (見Apple doc)。 Apple還提到,這對於擁有超過1000行的表視圖有一些性能問題。

最好的解決方案通常是爲每種類型的單元使用一個重用標識符。假設你有一個HighCell和一個LowCell,那麼你會找出你需要的當前行的單元格類型(在cellForRowAtIndexPath中),並將這種單元格類型出列。或者更好,使用- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifierApple doc

+0

我很抱歉,但你的回答中沒有任何消息給我。第一段確實包含我寫的內容,不是嗎? –

+0

您之前開始討論創建內容視圖。我試圖向你解釋你應該如何創建內容視圖。那是你的問題,不是嗎? – Rick

+0

不,這是關於具有動態行高的效率。 –

相關問題