开发者

heightForRowAtIndexPath always called twice

I am having a hard time understanding the flow of a UITableViewController. Basically here's my structure:

I have a tableViewController which of course houses a tableView. And here I use getCellContentView and cellForRowAtIndexPath to do all the work related to my cells. My cell has a few UILabels with their own frames, one for each orientation. I detect and perform all the or开发者_StackOverflow社区ientation changes and frame redraws properly. No issues there. So far so good.

Now I need heightForRowAtIndexPath to return 100 for landscape and say 200 for portrait orientation. My issue is that when I NSLog inside it, the method gets called twice for whatever reason when I rotate the tableView. Anyone know why the method is fired twice on rotation? I thought -reloadData might be the issue and I verified if i was calling the -reloadData on rotation twice somehow, but no. I call the -reloadData only once inside the willRotateToInterfaceOrientation method and nowhere else in the code is it called (except inside the first viewDidLoad call).

Why is the heightForRowAtIndexPath being called twice on each rotation event? It also appears that the heightForRowAtIndexPath is also unable to determine the orientation properly when the rotation kicks in. I use the statusbar orientation property to check that inside heightForRowAtIndexPath to return appropriate cell height. Any help would be awesome!


Who cares how many times it's being called :)

Your implementation of heightForRowAtIndex: should be fast enough that a table view can call it whenever it wants and it won't matter.

However, if I had to speculate, a rotation is done in two stages (look at didAnimateFirstHalfOfRotationToInterfaceOrientation: etc in the UIViewController documentation.) you have no idea what's being done by UIKit's UITableViewController class during a rotatino - maybe it's refreshing the table view itself for you?

Have you tried overriding willRotateToInterfaceOrientation: which should give you to orientation you are rotating to - could you use that to set the height of cell you want?

Something like this : (warning, untested and typed from memory!)

- (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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜