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

Iphone 当deviceOrientation更改时,在UITableView中重新加载数据,iphone,uitableview,landscape,Iphone,Uitableview,Landscape,我正在使用UITableView和UITableViewCell。 我的应用程序支持横向,因此我创建了2个UITableViewCell:一个用于portraid,另一个用于横向 在我的cellforrowatinexpath方法中,这是我的代码 static NSString *CellIdentifier; static NSString *NibNamed; UIDeviceOrientation deviceOrientation = [UIApplication sharedAppl

我正在使用UITableView和UITableViewCell。 我的应用程序支持横向,因此我创建了2个UITableViewCell:一个用于portraid,另一个用于横向

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

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中,我写道:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}
问题在哪里? 如果我在纵向模式下启动应用程序,当我第一次旋转设备时,什么也不会发生(UITableCellView是纵向\表格\单元格)。当我第二次旋转设备时(我处于纵向或向上向下),我会看到横向单元格(当然,我看不到所有文本,因为它从屏幕上消失)。 所以除了第一次,我旋转设备的其他时间,我看到了错误的表格\单元格\视图

你知道为什么吗?
谢谢

在设备更改方向之前,您的数据将重新加载。 在任何旋转之前调用shouldAutorotateToInterfaceOrientation

尝试didRotateFromInterfaceOrientation

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
 [my_table reloadData];
}
还要注意Jhaliya给出的答案,因为该解决方案比重新加载tableview更好。
仅当数据源已更改时,才应重新加载tableview。

不从
调用重新加载数据应自动旋转指向面方向:

为这两个对象创建对象时,对
UITableView
UITableViewCell
使用UIView
autoresizingMask
属性。因为
UITableView和
UITableViewCell
UIView`的子类

@property(nonatomic) UIViewAutoresizing autoresizingMask

我认为有两个问题。1) shouldAutoRotateToInterfaceOrientation在视图旋转之前被调用,因此如果当时刷新,则为时过早。 2) 我认为您需要自己管理重绘,而不是依赖于重新加载数据。因此,要么重写tableViewCell的重画方法,要么适当地设置自动调整大小的遮罩。希望这有帮助。
-Mike

如果自动调整大小不适合您,您还可以使用

-(NSArray*)InExpathForVisibleRow

然后把细胞本身拿出来

-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath

并调用一个类似

[单元格界面定向IDChangeTo:interfaceOrientation]


让细胞自己完成这项工作。

但请看一下@Jhaliya有什么要说的。最好是让你的细胞自动符合大小。