我有一个自定义 UITableViewCell,其中包含几个 UIButton.每个按钮的框架位置都与单元格宽度相关.我设置了 autoresizingMask=UIViewAutoresizingFlexibleWidth ,因此当应用程序以横向或纵向模式启动设备时,它将正确调整单元格宽度和按钮位置.
I have a custom UITableViewCell which contains several UIButtons. Each button's frame position is relative to the cell width. I set autoresizingMask=UIViewAutoresizingFlexibleWidth so it will adjust the cell width and the button positions properly when the application starts with the device either in landscape or portrait mode.
问题是当设备从一种模式旋转到另一种模式时,按钮不会调整位置,因为 UITableViewCell 是可重复使用的.换句话说,单元格没有根据新的 UITalbeView 宽度进行初始化,因为单元格的函数 initWithStyle 在设备旋转之前被调用,并且在设备旋转之后不再被调用.有什么建议吗?
The issue is when the device is rotated from one mode to the other, the buttons do not adjust positions because the UITableViewCell is reusable. In other words, the cell is not initialized based on the new UITalbeView width because the cell's function initWithStyle is called before the device is rotated and is not called again after the device rotation. Any suggestions?
经过数小时的研究(包括本站的帖子),我找不到任何解决方案.但是一个灯泡突然亮了起来.解决方案非常简单.只需检测设备方向是横向还是纵向模式,并为每个定义 ReusableCellIdentifier 并使用不同的名称.
After spending hours of research (including posts in this site), I could not find any solutions. But a light bulb turns on all of a sudden. The solution is very simple. Just detect whether the device orientation is landscape or portrait mode and define the ReusableCellIdentifier with a different name for each.
static NSString*Identifier;
if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) {
Identifier= @"aCell_portrait";
}
else Identifier= @"DocumentOptionIdentifier_Landscape";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
这篇关于旋转自定义 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!