swift 自动调整表视图

来源:互联网 发布:三国乱世战车升级数据 编辑:程序博客网 时间:2024/05/22 08:19

在iOS 7开发人员引入动态类型的概念——允许用户通过设置面板在程序中修改字体大小。这真是强大,可以大大改善用户体验为用户提供不同品质的视线。

然而,有一个很大的问题与此相关,在表视图的形式。虽然类型的大小可以改变,这是由开发人员定义的cell高度的表视图。这意味着要么内置值,或者动态地计算它们,都不很容易。当然一定有更好的方法吗?

在iOS 8终于可以表视图细胞可autosize本身。在这个项目中,我们将演示与动态类型和自定义相关的cell,看一看实现它是多么容易。

Using the ‘stock’ table view cells 

When you first create a table view, chances are you’ve just used the stock table view cells - ofwhich there are 4 styles (basic, left detail, right detail, and subtitle). In iOS 8, the labels in the baseUITableViewCellare pre-configured for dynamic type. This means that they adapt to the text sizespecified in the device settings panel. 

当您首先创建一个表视图,那么你只是使用了股票表视图细胞——其中有4风格(基本、左细节,正确的细节,和副标题)。在iOS 8中,标签在基地UITableViewCell预先配置的动态类型。这意味着他们适应文字大小设置面板中指定的设备。

Since these cells are also laid out using autolayout, they will autosize to fit the differing sizes of text. 

由于这些细胞也提出使用autolayout,他们将autosize以适应不同大小的文本。

This actually means that you get auto-sizing table view cellsfor freeif you just use the stock cells.

To see this in action run up the accompanyingMagicTableapp: 

这实际上意味着你得到免费使用自动调整表视图细胞如果你只是使用股票细胞。看到这在操作运行陪同MagicTable应用:


If you then use the settings to change the text size (in the same way as iOS7): 

如果你使用的设置更改文本大小(iOS7一样):

And then return to theMagicTableapp then you’ll be able to see the effect: 

然后返回到MagicTable应用然后你就能看到效果:


This is all well, and good, but more often than not you’ll want to create your own custom table cells.In order to do this, you need to understand a little bit more about how the auto sizing actually works- let’s take a look at that next. 

这都没问题,好,但往往你想要创建自己的自定义表cell。为了做到这一点,你需要了解一点关于自动大小实际上是如何工作的——让我们看看下一个。

Creating custom table view cells 

创建自定义表视图单元格

Traditionally setting the row height for a table view cell would be done on the table - using therowHeightproperty. In order to vary the row size on a per-row basis, you would use the table viewdelegate methodestimatedHeightForRowAtIndexPath:to return a different height for each row. 

传统上设置表视图单元格的行高将在table里——使用rowHeight属性。为了改变一行行大小,您将使用表格视图代表方法estimatedHeightForRowAtIndexPath:为每一行返回一个不同的高度。

The problem for this approach is that you either need to know the row height at compile time, orcalculate the row height for each row at run-time. The delegate method is called when the tableviewfirst appears, which means that you need to calculate the cell height beforethe cells are created. Inorder to do this you can end up writing layout code twice - once for size calculations and once fordisplay. This process can take a long time to perform - and involves entirely up-front calculations. 

这种方法的问题是,你要么需要在编译时知道行高,或在运行时计算每一行的行高。委托调用方法tableview首次出现时,这意味着你需要在cell创建之前计算cell高度。为了做到这一点,你可以最终布局代码写两次,一次一次计算和显示大小。这个过程可能需要很长时间来执行,包括完全预先计算。

iOS 7 introducedestimatedRowHeight, which transformed the row-height requests into lazycalculations - only requesting the height for a row once it is about to be displayed on the screen.However, it still required you to calculate the row height yourself. 

iOS 7介绍estimatedRowHeight,行高请求变成懒惰的计算——只有请求一行的高度一旦将显示在屏幕上。然而,它仍然需要你自己计算行高。

In iOS 8, you can still use these approaches, however, cells can now be responsible for their own sizing- via autolayout. This is both great from an ease-of-use perspective and also from a software designangle. A cell is responsible for its own layout, so it makes sense that it should also be responsible fordetermining its own height. 

在iOS 8中,你仍然可以使用这些方法,然而,cell现在可以自己的大小——通过autolayout负责。这既是伟大的从易用性的角度来看,也从软件设计的角度。cell负责自己的布局,所以它是有意义的,它也应该负责确定自己的高度。

It’s actually pretty easy to get auto-cell height working for a custom cell. The most important part is that your constraints properly define the height of the cell.

其实很容易得到auto-cell高度为一个自定义的工作单元。最重要的部分是你的约束正确定义单元格的高度。

You must provide an estimated height for the rows, and if you provide an actual height (either viathe property on the table, or via the delegate) then this will override any calculated cell size. In orderto specify that you haven’t set a cell height, use the UITableViewAutomaticDimensionconstant: 

行你必须提供一个估计的高度,如果你提供一个实际高度(通过属性表,或通过代表),那么这将会覆盖任何计算单元尺寸。为了指定你还没有设定一个细胞高度,使用UITableViewAutomaticDimension常数:

tableView.rowHeight=UITableViewAutomaticDimension

The constraints need to relate to thecontentViewwithin a UITableViewCell, and can be set up incode or in IB. The accompanying project sets up constraints in IB: 

需要的约束与contentView UITableViewCell,并且可以设置在代码或IB。伴随IB项目设置约束:


In order to demo the different heights, the datasource for the table inMagicTable, changes the font height of the custom label depending on the label: 

为了演示不同的高度,在MagicTable表的数据源,变化的字体高度自定义标签根据标签:


Conclusion 

结论

Auto-sizing table view cells is something that developers have longed for, and it’s great news thatiOS 8 introduces this functionality. In many cases, since you should already be using auto-layout,you’ll just get this functionality for free - just a matter of not specifying cell heights. It’s definitelyworth the time to go and ensure that your existing tableviews support this behavior. 

使用自动调整表视图细胞是开发人员渴望,这是个好消息,iOS 8介绍此功能。以来在许多情况下,您应该已经使用自动布局,你会免费获得此功能——只是没有指定单元格高度。这绝对是值得花时间去确保你现有的tableview支持这种行为。

















0 0
原创粉丝点击