HeightForRowAtIndexPath is always called twice

I find it difficult to understand the UITableViewController stream. Basically here is my structure:

I have a tableViewController which of course contains a tableView. And here I use getCellContentView and cellForRowAtIndexPath to do all the work related to my cells. My cell has several UILabels with their own frames, one for each orientation. I detect and perform all orientation changes and correctly redraw the frame. No problems. So far so good.

Now I need heightForRowAtIndexPath to return 100 for the landscape and say 200 for portrait orientation. My problem is that when I have an NSLog inside it, the method is called twice for some reason when I rotate the tableView. Does anyone know why the method runs twice during rotation? I thought that -reloadData could be a problem, and I confirmed that I was calling -reloadData when spinning twice somehow, but not. I call -reloadData only once in the willRotateToInterfaceOrientation method and it is not called anywhere in the code (except for the internal call to viewDidLoad).

Why is heightForRowAtIndexPath called twice in every rotation event? It also seems that heightForRowAtIndexPath also cannot correctly determine the orientation when turning on rotation. I use the orientation property of the status bar to verify that inside heightForRowAtIndexPath returns the corresponding cell height. Any help would be awesome!

+3
source share
1 answer

Who cares how many times it is called :)

Your implementation of heightForRowAtIndex: should be fast enough so that the table view can call it whenever it wants, and that doesn't matter.

, , ( didAnimateFirstHalfOfRotationToInterfaceOrientation: etc UIViewController.), , 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

Source: https://habr.com/ru/post/1780701/


All Articles