2013-12-12 39 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

不,这是关于具有动态行高的效率。 –

相关问题