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 mainBund开发者_运维百科le] 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!
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.
Not call reloadData from shouldAutorotateToInterfaceOrientation
:
Use the UIView autoresizingMask
property for both UITableView
and UITableViewCell
when you create the object for both . because UITableView and
UITableViewCellare the subclass of
UIView`.
@property(nonatomic) UIViewAutoresizing autoresizingMask
I think there are two problems. 1) shouldAutoRotateToInterfaceOrientation is called BEFORE the view rotates, so if you refresh then, it's too early. 2) I think you need to manage the redraw yourself rather than rely on reloadData. So either override the redraw method of the tableViewCell, or set your autoresizing masks appropriately. Hope this helps. -Mike
If the autosizing doesn't suit you well, you could also get the visible cells indexes with
- (NSArray *)indexPathsForVisibleRow
And then get the cells itself with
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
And call a method like
[cell interfaceOrientationDidChangeTo:interfaceOrientation]
And let the job be done by the cell itself.
精彩评论