Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reloadData in a UITableView when deviceOrientation change

I'm working with a UITableView, with UITableViewCell. My appsupports the landscape orientation so i've created 2 UITableViewCell: one for the portraid and one for the landscape.

In my cellForRowAtIndexPath method this is my code

static NSString *CellIdentifier;
static NSString *NibNamed;

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (deviceOrientation != UIDeviceOrientationLandscapeLeft &&
    deviceOrientation != UIDeviceOrientationLandscapeRight)
{
    CellIdentifier= @"Cell1";
    NibNamed = @"cell_news";
}
else
{
    CellIdentifier= @"Cell2";
    NibNamed = @"cell_news_landscape";
}

[[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:NULL];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
///here i add title, description, ecc... in my UITableViewCell

Then in shouldAutorotateToInterfaceOrientation i wrote this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}

Where is the problem? If i start the app in portrait, when i rotate the device the first time, nothing happen (and the UITableCellView is the portrait_table_cell). When i rotate the device for the second time (and i'm in portrait or in upsideDown), i see the landscape_table_cell (and, of course, i don't see all the text because it goes out from the screen). So.. except the firt time, the other times that i rotate the device, i see the wrong table_cell_view!!

Do you know why? Thanks!

like image 238
JAA Avatar asked Apr 14 '11 14:04

JAA


2 Answers

Your data is reload before the device changed orientation. shouldAutorotateToInterfaceOrientation is called before any rotation.

Try the didRotateFromInterfaceOrientation

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
 [my_table reloadData];
}

Also note the answer given by Jhaliya, because that solution will be better then reloading you tableview. You should only reload the tableview if the datasource has changed.

like image 89
rckoenes Avatar answered Sep 25 '22 16:09

rckoenes


Not call reloadData from shouldAutorotateToInterfaceOrientation:

Use the UIView autoresizingMask property for both UITableView and UITableViewCell when you create the object for both . because UITableView andUITableViewCellare the subclass ofUIView`.

@property(nonatomic) UIViewAutoresizing autoresizingMask
like image 21
Jhaliya - Praveen Sharma Avatar answered Sep 23 '22 16:09

Jhaliya - Praveen Sharma