旋转自定义 UITableViewCell

Rotate a custom UITableViewCell(旋转自定义 UITableViewCell)
本文介绍了旋转自定义 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
Question about the rotation of X axis on Android(关于Android上X轴旋转的问题)