【问题标题】:Rotate a custom UITableViewCell旋转自定义 UITableViewCell
【发布时间】:2011-01-29 10:55:21
【问题描述】:

我有一个自定义 UITableViewCell,其中包含几个 UIButton。每个按钮的框架位置都与单元格宽度相关。我设置了 autoresizingMask=UIViewAutoresizingFlexibleWidth ,因此当应用程序以横向或纵向模式启动设备时,它将正确调整单元格宽度和按钮位置。

问题是当设备从一种模式旋转到另一种模式时,按钮不会调整位置,因为 UITableViewCell 是可重复使用的。换句话说,单元格没有根据新的 UITalbeView 宽度进行初始化,因为单元格的函数 initWithStyle 在设备旋转之前被调用,并且在设备旋转之后不再被调用。有什么建议吗?

【问题讨论】:

  • 最终解决方案要简单得多。它只需要设置 self.contentView.autoresizingMask=UIViewAutoresizingFlexibleWidth; self.contentView.autoresizesSubviews=YES;设置 self.autoresizingMask 不起作用。
  • 我和你有同样的问题,我正在考虑为每个方向案例使用特定的单元格标签,因为这个想法是在我思考了一段时间后想到的。使用 self.contentView.autoresizesSubviews=YES 对我来说不起作用。
  • 最后我用了另一个技巧。

标签: iphone uitableview rotation reusability initwithstyle


【解决方案1】:

勾选的答案在旧 iOS 版本中就像一个魅力。对于 iOS 6.0,我使用了以下代码:

static NSString *Identifier;
if (self.interfaceOrientation==UIInterfaceOrientationPortrait) {
    Identifier=@"aCell_portrait";
}
else {
    Identifier=@"DocumentOptionIdentifier_Landscape";
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,这篇文章帮助了我。在我的情况下,我在一个单独的文件中声明了一个自定义类,在这个文件中,我在layoutSubviews 中有以下代码:

    //PORTRAIT CELL
    if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && 
        [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight)
    {
        //build the custom content views for portrait mode here
    }
    else
    {
        //build the custom content views for landscape mode here
    }
    

    然后在我的视图控制器中,我只需实现willAnimateRotationToInterfaceOrientation: 并将reloadData 消息发送到我的表格视图。

    有了这个我就不用碰cellForRow方法了。

    【讨论】:

    • 使用layoutSubviews你不需要使用reloadData,最好不要使用它,因为如果你在编辑模式下显示删除按钮,然后旋转,删除由于重新加载数据,按钮将消失
    【解决方案3】:

    由于 UITableViewCell 也是一个 UIView,你可以重写 setFrame 方法。每次您的表格视图旋转时,都会为所有单元格调用此方法。

    -(void)setFrame:(CGRect)frame
    {
        [super setFrame:frame];
    
        //Do your rotation stuffs here :)
    } 
    

    【讨论】:

      【解决方案4】:

      您需要在 cellForRowAtIndexPath 方法中修复单元格框架宽度(假设纵向和横向模式下的高度相同)。这就是这里的工作。我曾经用 IB 创建一个自定义 TableViewCell,它总是初始化为纵向 320 像素宽度。通过定义框架,它可以按预期工作,即使单元格从队列中“重用”。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       ...
       UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
       if (cell == nil) {
          // create cell here...
       }
      
       // Adjust cell frame width to be equal to tableview frame width
       cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height);
       ...
      }
      

      【讨论】:

        【解决方案5】:

        上一个答案有一个严重的问题。 您应该使用 [UIApplication sharedApplication].statusBarOrientation 而不是 [UIDevice currebtDevice].orientation,因为设备方向与界面方向无关 - 设备方向是基于加速度计的物理旋转。

        【讨论】:

        • 感谢您告知第二个选项。事实上,两者都可以工作。见stackoverflow.com/questions/2738734/…
        • 对不起,你说的不对。设备方向有 6 种不同的选项,而界面方向有 4 种。如果您的设备平放在桌子上,设备方向将报告“Face Up”,这与当前界面方向没有任何共同之处。
        【解决方案6】:

        经过数小时的研究(包括本网站上的帖子),我找不到任何解决方案。但是一个灯泡突然亮了起来。解决方案非常简单。只需检测设备方向是横向还是纵向模式,并为每个定义 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];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-26
          • 1970-01-01
          • 2015-11-13
          • 2011-03-08
          • 2013-11-28
          相关资源
          最近更新 更多