当deviceOrientation改变时,在UITableView中重新加载数据

JAA*_*JAA 5 iphone landscape uitableview

我正在使用带有UITableViewCell的UITableView.我的应用程序支持横向,所以我创建了2个UITableViewCell:一个用于portraid,另一个用于横向.

在我的cellForRowAtIndexPath方法中,这是我的代码

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
Run Code Online (Sandbox Code Playgroud)

然后在shouldAutorotateToInterfaceOrientation我写了这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?如果我以纵向启动应用程序,当我第一次旋转设备时,什么也没发生(并且UITableCellView是portrait_table_cell).当我第二次旋转设备时(我是在纵向或在upsideDown中),我看到landscape_table_cell(当然,我没有看到所有文本,因为它从屏幕上消失).所以..除了第一次,其他时间我旋转设备,我看到错误的table_cell_view !!

你知道为什么吗?谢谢!

rck*_*nes 9

在设备更改方向之前,您的数据会重新加载.在任何轮换之前调用shouldAutorotateToInterfaceOrientation.

尝试didRotateFromInterfaceOrientation

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
 [my_table reloadData];
}
Run Code Online (Sandbox Code Playgroud)

还要注意Jhaliya给出的答案,因为该解决方案会比重新加载tableview更好.如果数据源已更改,则只应重新加载tableview.