【问题标题】:size class for table row height in iOS storyboardiOS故事板中表格行高的大小类
【发布时间】:2016-01-08 04:29:03
【问题描述】:

我使用 Swift 和 Storyboard 开发了一个 iOS 应用。我放置了一个表格,我需要根据屏幕类更改表格单元格的高度,但我在表格选项中找不到这样的选项。我看到单元格高度是由 Table View / Row height 选项定义的,但它不支持类大小。

出现这个问题是因为我在单元格原型中使用尺寸等级设置标签的字体,所以对于更大的屏幕,字体更大,但每个尺寸等级不能更改行高。显然,我需要更高的行来获得更大的字体。我应该使用什么方法来实现它?

Obj-C 代码也可以。

【问题讨论】:

    标签: ios objective-c swift storyboard interface-builder


    【解决方案1】:

    让我回答我自己的问题 - 在 iOS 8 中,可以使用 UITableViewAutomaticDimension 为表格的行高启用单元格的自动布局:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.estimatedRowHeight = 89
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    

    estimatedRowHeight也需要设置。

    链接:

    http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/

    http://www.appcoda.com/self-sizing-cells/

    https://stackoverflow.com/a/28918600/1028256

    【讨论】:

      【解决方案2】:

      使用 UITableView 委托通过代码设置它。

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
              //check the conditions and return the row height.
      
                  return 71.0;
      }
      

      您也可以在 Swift 中找到类似的委托。

      【讨论】:

      • 如果在情节提要中无法做到这一点,我可以接受这个答案。另外,如何在代码中检查当前使用的屏幕尺寸类?
      猜你喜欢
      • 2014-12-20
      • 1970-01-01
      • 2013-07-26
      • 2019-01-16
      • 2015-07-18
      • 1970-01-01
      • 2014-07-27
      • 2012-10-11
      • 2016-12-18
      相关资源
      最近更新 更多