旋转iPhone时重置自定义UITableViewCell高度 [英] Resetting custom UITableViewCell height when iPhone is rotated

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

问题描述

我正在重载委托方法 -tableView:heightForRowAtIndexPath:并使用 -sizeWithFont:constrainedToSize:以编程方式设置单元格的高度,基于该单元格中的文本:

I am overloading the delegate method -tableView:heightForRowAtIndexPath: and using -sizeWithFont:constrainedToSize: to programmatically set the height of the cell, based on the text in that cell:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  switch (indexPath.section) {
    case(kAboutViewOverviewSection): {
      switch (indexPath.row) {
        case(kAboutViewOverviewSectionRow): {
          NSString *text = NSLocalizedString(@"kAboutViewOverviewSectionFieldText", @"");
          CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(280, 500)];
          return s.height + 13;
        }
        default: {
          break;
        }
      }
      break;
    }
    default: {
      break;
    }
  }
  return 44.0;
}

这在第一次绘制表时有效。但是,当我更改设备的方向时,从纵向到横向,单元格高度不会改变。

This works when the table is first drawn. However, when I change the orientation of the device, from portrait to landscape, the cell height does not change.

我试图覆盖 -shouldAutorotateToInterfaceOrientation :我的表视图控制器中的方法:

I tried overriding the -shouldAutorotateToInterfaceOrientation: method in my table view controller:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  [self.tableView reloadData];
  return YES;
}

这应该重新加载表数据并且(可能)重绘表格单元格视图。但这没有任何影响。

This should reload the table data and (presumably) redraw the table cell views. But this does not have any effect.

当设备旋转时,有没有办法强制单元格重新绘制高度?

Is there a way to force a cell to redraw with a new height, when the device is rotated?

编辑:我尝试了以下操作,但没有效果:

EDIT: I have tried the following, to no effect:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  [self.tableView reloadData];
}

这是应用程序在纵向方向的样子:

Here is what the application looks like in portrait orientation:

以下是横向应用程序的样子:

Here is what the application looks like in landscape orientation:

内容的上边缘和下边缘之间存在间隙。

There is a gap between the top and bottom edges of the content.

编辑2

通过表格框架的宽度调整尺寸参数有助于解决此显示问题:

Adjusting the size parameter via the table frame's width helped fix this display issue:

CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width,500)];


推荐答案

这不是最优的,但最简单的解决方案是在桌面上调用 -reloadData

It's less than optimal, but the simplest solution is to call -reloadData on the table.

更好的解决方案是调用 -beginUpdates -deleteRowsAtIndexPaths:withRowAnimation: -insertRowsAtIndexPaths:withRowAnimation:,和 -endUpdates 或只是 -reloadSections:withRowAnimation:如果仅定位3.0。这将添加动画。

A better solution would be to call -beginUpdates, -deleteRowsAtIndexPaths:withRowAnimation:, -insertRowsAtIndexPaths:withRowAnimation:, and -endUpdates or simply -reloadSections:withRowAnimation: if targeting 3.0 only. This will add animation.

编辑:你还需要一个正确的 tableView:heightForRowAtIndexPath:

And you will also need a proper tableView:heightForRowAtIndexPath:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGSize textSize = [[self textForRowAtIndexPath:indexPath] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)];
    return textSize.height + 13.0f;
}

(其中 textForRowAtIndexPath:是一个返回单元格文本的方法)

(where textForRowAtIndexPath: is a method that returns the cell's text)

这篇关于旋转iPhone时重置自定义UITableViewCell高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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