旋转自定义UITableViewCell [英] Rotate a custom UITableViewCell

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

问题描述

我有一个自定义UITableViewCell,它包含几个UIButtons。每个按钮的框架位置相对于单元格宽度。我设置了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆