问题描述
|
我正在使用带有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
然后在shouldAutorotatetoInterfaceOrientation中,我这样写:
- (BOOL)shouldAutorotatetoInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return YES;
}
问题出在哪里?
如果我以纵向启动应用程序,则在第一次旋转设备时,什么也不会发生(UITableCellView是portrait_table_cell)。当我第二次旋转设备时(并且我处于纵向或倒置状态),我看到了landscape_table_cell(当然,我看不到所有文本,因为它从屏幕上消失了)。
所以..除了点火时间,其他时间我旋转设备,我看到了错误的table_cell_view!
你知道为什么吗?
谢谢!
解决方法
在设备更改方向之前,将重新加载数据。
应该在任何旋转之前调用shouldAutorotateToInterfaceOrientation。
尝试didRotateFromInterfaceOrientation
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[my_table reloadData];
}
还要注意Jhaliya给出的答案,因为与重新加载tableview相比,该解决方案会更好。
如果数据源已更改,则仅应重新加载tableview。
,不从shouldAutorotateToInterfaceOrientation
调用reloadData:
在同时为ѭ5for和UITableViewCell
创建对象时,请同时使用UIViewautoresizingMask
属性。因为UITableView and
UITableViewCellare the subclass of
UIView`。
@property(nonatomic) UIViewAutoresizing autoresizingMask
,我认为有两个问题。 1)在视图旋转之前应该调用AutoAutoRotateToInterfaceOrientation,因此如果刷新,则为时过早。
2)我认为您需要自己管理重绘,而不是依赖reloadData。因此,要么重写tableViewCell的redraw方法,要么适当地设置自动调整大小的蒙版。希望这可以帮助。
-麦克风
,如果自动调整大小不适合您,您还可以使用
- (NSArray *)indexPathsForVisibleRow
然后用
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
并调用类似
[cell interfaceOrientationDidChangeTo:interfaceOrientation]
并让工作由单元本身完成。