2010-12-16 32 views
3

我很难理解UITableViewController的流程。基本上这里是我的结构:heightForRowAtIndexPath总是调用两次

我有一个tableViewController哪个当然有一个tableView。在这里我使用getCellContentView和cellForRowAtIndexPath来完成与我的单元相关的所有工作。我的单元格有几个UILabels和它们自己的框架,每个方向都有一个。我检测并执行所有方向更改并正确重画框架。那里没有问题。到现在为止还挺好。

现在我需要heightForRowAtIndexPath为纵向返回100,纵向为200。我的问题是,当我NSLog里面,当我旋转tableView时,无论什么原因,该方法被调用两次。任何人都知道为什么该方法在旋转时被触发两次?我认为-reloadData可能是问题,我验证了我是否以某种方式调用了-reloadData两次,但没有。我在willRotateToInterfaceOrientation方法中只调用了一次-reloadData,并且调用了代码中的其他地方(第一个viewDidLoad调用除外)。

为什么heightForRowAtIndexPath在每个旋转事件上被调用两次?它也似乎heightForRowAtIndexPath也无法正确地确定方向时旋转踢英寸我使用状态栏方向属性来检查内部heightForRowAtIndexPath返回适当的单元格高度。任何帮助都是极好的!

回答

0

谁在乎它多少次被称为:)

你实现heightForRowAtIndex的:应该是足够快,只要它想表视图可以调用它,它都不会有问题。但是,如果我必须推测,旋转是分两个阶段完成的(请参阅UIViewController文档中的didAnimateFirstHalfOfRotationToInterfaceOrientation:etc)。您不知道UIKit的UITableViewController类在旋转过程中会做什么 - 也许它正在刷新桌子视图本身为你?

您是否尝试过重写willRotateToInterfaceOrientation:它应该能够让您定位旋转到的方向 - 可以使用它来设置所需单元格的高度?

事情是这样的:

- (NSInteger)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return rowHeight; 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { 
    [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration]; 

    rowHeight = (UIInterfaceOrientationPortrait == interfaceOrientation) ? 100 : 200; 
    [tableView reloadData]; 
} 
+0

deanWombourne先生,我爱你(警告,未经测试并从内存类型!)!成功了!现在我正在阅读旋转内部函数,以便真正弄清楚为什么表格会被渲染两次(就像创建了两个reloadData调用一样)。 – user511872 2010-12-17 08:17:38