当deviceOrientation改变时,在UITableView中重新加载数据 [英] reloadData in a UITableView when deviceOrientation change
问题描述
我正在使用带有UITableViewCell的UITableView。
我的apps支持横向,所以我创建了2个UITableViewCell:一个用于portraid,一个用于横向。
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.
在我的cellForRowAtIndexPath方法中,这是我的代码
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
然后在shouldAutorotateToInterfaceOrientation我写了这个:
Then in shouldAutorotateToInterfaceOrientation i wrote this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}
问题出在哪里?
如果我以纵向启动应用程序,当我第一次旋转设备时,没有任何反应(UITableCellView是portrait_table_cell)。当我第二次旋转设备时(我是在纵向或在upsideDown中),我看到landscape_table_cell(当然,我没有看到所有文本,因为它从屏幕中消失)。
所以..除了第一次,其他时间我旋转设备,我看到错误的table_cell_view !!
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!
推荐答案
您的数据会在设备更改方向之前重新加载。在任何轮换之前调用
shouldAutorotateToInterfaceOrientation。
Your data is reload before the device changed orientation. shouldAutorotateToInterfaceOrientation is called before any rotation.
尝试didRotateFromInterfaceOrientation
Try the didRotateFromInterfaceOrientation
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[my_table reloadData];
}
还要注意Jhaliya给出的答案,因为该解决方案会比重新加载更好你是tableview。
如果数据源已更改,则只应重新加载tableview。
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.
这篇关于当deviceOrientation改变时,在UITableView中重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!