Way*_* Lo 7 iphone rotation reusability uitableview initwithstyle
我有一个自定义UITableViewCell,其中包含几个UIButtons.每个按钮的框架位置相对于单元格宽度.我设置了autoresizingMask = UIViewAutoresizingFlexibleWidth,因此当应用程序以横向或纵向模式启动时,它将正确调整单元格宽度和按钮位置.
问题是当设备从一种模式旋转到另一种模式时,按钮不会调整位置,因为UITableViewCell是可重复使用的.换句话说,基于新的UITalbeView宽度不会初始化单元格,因为在设备旋转之前调用单元格的函数initWithStyle,而在设备旋转之后不再调用该单元格.有什么建议?
小智 13
由于UITableViewCell也是UIView,因此您可以覆盖setFrame方法.每次表视图旋转时,都会为所有单元格调用此方法.
-(void)setFrame:(CGRect)frame
{
[super setFrame:frame];
//Do your rotation stuffs here :)
}
Run Code Online (Sandbox Code Playgroud)
经过数小时的研究(包括本网站的帖子)后,我找不到任何解决方案.但是一个灯泡突然开启了.解决方案非常简单.只需检测设备方向是横向还是纵向模式,并为每个设置定义具有不同名称的ReusableCellIdentifier.
static NSString*Identifier;
if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) {
Identifier= @"aCell_portrait";
}
else Identifier= @"DocumentOptionIdentifier_Landscape";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
Run Code Online (Sandbox Code Playgroud)
以前的答案有一个严重的问题.您应该使用[UIApplication的sharedApplication] .statusBarOrientation而非[的UIDevice currebtDevice] .orientation因为设备的方向无关,与接口方向 - 设备方向是基于加速度传感器的物理旋转.
归档时间: |
|
查看次数: |
11587 次 |
最近记录: |