具有静态表的动态类型和自定尺寸单元 [英] Dynamic Type and Self-Sizing Cells with Static Table

查看:134
本文介绍了具有静态表的动态类型和自定尺寸单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个典型的主 - 细节应用程序,允许用户浏览滚动的对象列表,然后使用push segue钻取到任何特定对象的细节。滚动主列表是使用原型单元格构建的UITableView,细节场景是一个静态UITableView,具有固定数量的节和单元格。

I have a typical master-detail app that allows the user to browse a scrolling list of objects, and then drill to detail for any particular object with a push segue. The scrolling master list is a UITableView built with prototype cells, and the detail scene is a static UITableView with a fixed number of sections and cells.

我想实现Dynamic在我的应用程序中键入和自定义单元格,以便用户可以更改基本字体大小。到目前为止,我已成功使用原型单元格的滚动列表制作自调整单元格:使用自动布局,将每个标签中的行数设置为0,并设置 tableView.rowHeight = UITableViewAutomaticDimension ,每个原型单元格的高度增大或缩小以适应其中文本的大小。

I would like to implement Dynamic Type and Self-Sizing Cells in my app so that the user can change the base font size. So far, I have been successful making self-sizing cells with the scrolling list of prototype cells: by using Auto Layout, setting number of lines in each label to 0, and setting tableView.rowHeight = UITableViewAutomaticDimension, the height of each prototype cell grows or shrinks to accommodate the size of the text within.

但我无法达到同样的效果静态表视图。无论我使用自定义单元格还是内置单元格类型,字体都会增大/缩小,但单元格高度却没有。

But I can't achieve the same effect in my static table view. Whether I use custom cells or built-in cell types, the font grows/shrinks but the cell height does not.

所以我的问题实际上是两个问题:1)是可以在静态表视图中实现自调整单元格,就像我已经完成了原型表视图一样? 2)如果第一个问题的答案为否,我如何编写测量静态表格视图单元格中标签高度的代码并适当调整单元格高度?

So my question is actually two questions: 1) is it possible to implement self-sizing cells in static table views, like I've done with my prototype table view? and 2) if the answer to the first question is no, how do I write code that will measure the height of a label in a static table view cell and adjust the cell height appropriately?

谢谢!

推荐答案

我很感谢昨天发布的问题的答案。不幸的是(可能是因为我自己作为iOS程序员缺乏经验)我无法使systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)方法工作。然而,在经过一些试验和错误之后,我发现了一种似乎有用的不同方法:

I'm grateful for the answers provided to the question I posted yesterday. Unfortunately (and likely because of my own inexperience as an iOS programmer) I was not able to make the systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) approach work. After some trial and error, however, I found a different method that seems to work:

import UIKit

class MasterTVC: UITableViewController {
    @IBOutlet weak var staticCustomCellLabel: UILabel!

    //viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.estimatedRowHeight = 44.0

        staticCustomCellLabel.text = "This is the text for the static custom cell label; This is the text for the static custom cell label; This is the text for the static custom cell label"

        //Register handleDynamicTypeChange in observer: self
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleDynamicTypeChange:", name: UIContentSizeCategoryDidChangeNotification, object: nil)
    }

    //deinit
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    //handleDynamicTypeChange
    //called when user changes text size in Settings > General > Accessibility > Larger Text
    //or Settings > Display & Brightness > Text Size
    func handleDynamicTypeChange(notification: NSNotification) {
        staticCustomCellLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)

        self.tableView.reloadData()
    }

    //tableView:heightForRowAtIndexPath
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
}

在故事板上此外,设置从具有以下属性的单个表视图控制器开始:

On the Storyboard side of this, the setup begins with a single Table View Controller with the following properties:


  • 类:MasterTVC(我的自定义类);

  • 指定了初始视图控制器;

  • 内容:静态单元格;

  • 样式:分组

  • 章节:1。

  • Class: MasterTVC (my custom class);
  • designated the initial view controller;
  • Content: Static Cells;
  • Style: Grouped
  • Sections: 1.

第1节:


  • 行数:1。

表格单元格:


  • 样式:自定义(在此处使用Basic会导致标签在设置中更改文本大小时消失);

  • 包含UILabel。

UILabel进一步配置为:

The UILabel is further configured as:


  • 字体:正文(动态类型样式);

  • 行数:0;

  • 固定在顶部,底部,左侧和内容视图的右边缘(我使用过各自的约束8,8,15,15);

  • 在自定义类代码中带有@IBOutlet。

  • Font: Body (a Dynamic Type style);
  • Lines: 0;
  • Pinned on all four sides to the top, bottom, left, and right edges of the content view (I used the respective constraints 8, 8, 15, 15);
  • with an @IBOutlet in the custom class code.

这适用于我在Xcode 7中设计iOS 9和启用了自动布局的Swift 2。有趣的是,如果你去除用于立即响应文本大小变化的NSNotificationCenter代码,自调整单元格代码基本上是两行:在viewDidLoad中设置estimatedRowHeight,并从tableView:heightForRowAtIndexPath返回UITableViewAutomaticDimension。

This worked for me designing for iOS 9 in Xcode 7 and Swift 2 with Auto Layout enabled. The interesting thing is that if you strip away the NSNotificationCenter code that is used to respond immediately to changes in text size, the self-sizing cells code is basically two lines: setting estimatedRowHeight in viewDidLoad, and returning UITableViewAutomaticDimension from tableView:heightForRowAtIndexPath.

根据这些结果,我相信我原来问题的答案是:1)是; 2)见1.

Based on these results, I believe the answers to my original question are: 1) yes; and 2) see 1.

这篇关于具有静态表的动态类型和自定尺寸单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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