views:

70

answers:

2

Hi,

I added the interfaceOrientation to my app. It works fine concerning the views. Some of the table-cells I defined by CGRects to position the text in the cell. In portrait-mode the cell is 300px long, in landscape-mode 420px. I use the following code to change the CGRects depending the orientation:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.interfaceOrientation == UIDeviceOrientationPortrait) {
        NSString *currentLanguage = [[NSString alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/sprache.txt"]]; 
        static NSString *TableViewTableCellIdentifier = @"TableViewTableCellIdentifier";
        UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:TableViewTableCellIdentifier];

        CGRect cellRect = CGRectMake(0, 0, 300, 175);
        cell.backgroundColor = [UIColor darkGrayColor];
        cell = [[[UITableViewCell alloc] initWithFrame:cellRect reuseIdentifier:TableViewTableCellIdentifier] autorelease];

        CGRect keyLabelRect = CGRectMake(0, 5, 5, 20);
        UILabel *keyLabel = [[UILabel alloc]initWithFrame:keyLabelRect];
        keyLabel.tag = 100; //.........
    } else {
        NSString *currentLanguage = 
            [[NSString alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/sprache.txt"]];   
        static NSString *TableViewTableCellIdentifier = @"TableViewTableCellIdentifier";

        UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:TableViewTableCellIdentifier];

        CGRect cellRect = CGRectMake(0, 0, 450, 175);
        cell.backgroundColor = [UIColor darkGrayColor];
        cell = [[[UITableViewCell alloc] initWithFrame:cellRect reuseIdentifier:TableViewTableCellIdentifier] autorelease];

        CGRect keyLabelRect = CGRectMake(0, 5, 5, 20);
        UILabel *keyLabel = [[UILabel alloc] //.....
    }
}

My problem is, when the table is visible and the orientation is changed, I need to scroll to see the new "layout". How can I manage to "reload" the view after changing the orientation?

A: 

I'm pretty sure that you do not need to specify frame during default UITableViewCell construction. Usually frame size is handled by UITableView itself.

But if you wish you can send setNeedsLayout and/or setNeedsDisplay message to a tableView to force it update cell layout (in a orientation handler).

Alexander Babaev
I tried both. But nothing changed. I need still to scroll when changed the orientation to replace the CGRects of portrait to the CGRects of landscape. I need the CGRects to place text at different places in a default tableviewcell.
Christian
A: 

First of all, this will leak like mad. You create cells and dequeue them but you never release them. You just create an entirely new cell every time a cell is requested. There is no reason to recreate a cell if you dequeue and vice versa. More importantly, your creating as many cells as you have rows in your logical table. That will eat all your memory very quickly.

When you dequeue a cell, you need to check if it's frame is the right size and reuse it if it is. If it is not, then you need to release the cell and create another one.

Second, a table will not ask for new cells until you scroll the existing cells off the screen. This is why your cells do not change until you scroll. It's the expected behavior of a tableview.

My standing recommendation for any moderately complex view is to use different view-controller/view pairs for each orientation. It seems like more work but usually I find it it actually takes less and it's easier to manage. In this case, having two separate tables will probably save you a lot of grief.

TechZen
Your recommendation with different view-controllers works fine. Thanks. But how do you manage when the user change the orientation? I decide in the viewcontroller0 the orientation: portrait -> viewControllerA, landscape -> viewControllerB. For example the viewControllerA was selectet and then the user change the orientation. He has to quit the actual viewControllerA and then the correct viewControllerB will be choosen.
Christian
In the `willRotateToOrientation` method of each controller you push the view controller for that orientation on to the navigation stack with no animation. When you rotate back, you pop the last view off. The user can't tell they are different views.
TechZen