Last year massive improvements like diffable data source and compositional layout were pushed to UICollectionView
. UICollectionView
didn’t exactly make its way into SwiftUI during this year’s WWDC, but that didn’t stop UICollectionView
from receiving some powerful new updates. With the introduction of UICollectionViewCompositionalLayout.list
and UICollectionViewListCofiguration
, UICollectionView
has become even more powerful and more customisable to use.
去年,诸如可扩散数据源和组合布局之类的大规模改进被推到了UICollectionView
。 在今年的WWDC期间, UICollectionView
并未完全进入SwiftUI,但这并未阻止UICollectionView
接收一些强大的新更新。 随着UICollectionViewCompositionalLayout.list
和UICollectionViewListCofiguration
的引入, UICollectionView
变得更加强大并且可以自定义使用。
When paired with UICollectionViewListCell
, UICollectionView
now support features that used to only be available to UITableView
only. For instance, now you can now add swipe actions and set its accessories on a cell like a disclosure indicator.
与UICollectionViewListCell
配对UICollectionViewListCell
, UICollectionView
现在支持以前仅可用于UITableView
。 例如,现在您可以添加滑动动作并将其附件设置在单元格上,如显示指示器。
In the next few sections, we’ll see how one can construct layouts and cells in UICollectionView
in the new iOS 14.
在接下来的几节中,我们将看到如何在新的iOS 14中的UICollectionView
中构造布局和单元格。
设置事情 (Setting Things Up)
Let’s say we are working on a view to render a list of contacts. The contacts that will be displayed in the list are modelled as struct
with three properties:
假设我们正在研究一种呈现联系人列表的视图。 将在列表中显示的联系人被建模为具有三个属性的struct
:
The collection view’s sections are modelled using an enum
Section:
集合视图的部分使用enum
Section建模:
In the view controller ContactsViewController, create three properties — a view model, that contains the data, a UICollectionView
and a UICollectionViewDataSource
to render the data.
在视图控制器ContactsViewController中,创建三个属性-一个包含数据的视图模型,一个UICollectionView
和一个UICollectionViewDataSource
来呈现数据。
UICollectionLayoutListConfiguration (UICollectionLayoutListConfiguration)
At WWDC 2019, Apple introduced the new Compositional layout API, a composable and flexible, to you build various kinds of layouts by combining or compositing smaller building blocks. Layouts can be easily created for common list variants such as plain
, grouped
and insetGrouped
, which enables us to quickly configure a list-like collection view.
在WWDC 2019上,Apple引入了可组合且灵活的新合成布局API,您可以通过组合或合成较小的构建基块来构建各种布局。 布局可以为共同的列表中很容易地创建为变体, plain
, grouped
和insetGrouped
,这使我们能够快速配置列表类似的集合视图。
单元配置和注册 (Cell Configuration and Registration)
iOS 14 introduces a brand new Cell Configuration API, UICollectionView.CellRegistration
to configure UICollectionView and UITableView cells.
iOS 14引入了全新的单元配置API UICollectionView.CellRegistration
来配置UICollectionView和UITableView单元。
Cell registrations provide a new, much more type-safe way to tell UIKit what UICollectionViewCell
subclass to use to render a given cell, and using a cell’s defaultContentConfiguration
, we can bind our data to it without having to be aware of its exact subviews.
单元格注册提供了一种新的类型安全性更高的方法,该方法可以告诉UIKit使用哪个UICollectionViewCell
子类来呈现给定的单元格,并且使用单元格的defaultContentConfiguration
,我们可以将数据绑定到它,而不必知道其确切的子视图。
Here’s how we could combine these two new APIs to create a CellRegistration
that uses the new UICollectionViewListCell
to render our contact list’s various cells:
这是我们如何结合这两个新的API来创建一个CellRegistration
,它使用新的UICollectionViewListCell
来呈现我们的联系人列表的各个单元格:
Apart from increased type safety, another major benefit of the above new API is that we can perform all of our cell configuration, as well as our swipe action event handling, at one place within a single closure.
除了提高类型安全性之外,上述新API的另一个主要优点是,我们可以在单个闭包内的一个位置执行所有单元配置以及滑动动作事件处理。
构造和更新可扩散数据源 (Constructing and Updating the Diffable Data Source)
To construct our list’s data source, we’ll use the built-in Diffable data source API that was introduced last year, in combination with the CellRegistration
instance that we built above, like this:
为了构造列表的数据源,我们将使用去年引入的内置Diffable数据源API以及上面构建的CellRegistration
实例,如下所示:
To populate the contact list data we’ll first create a Section
enum to describe our list’s two sections, and we’ll then insert the data from our viewModel
into those two sections using the NSDiffableDataSourceSnapshot
API:
为了填充联系人列表数据,我们将首先创建一个Section
枚举来描述列表的两个部分,然后使用NSDiffableDataSourceSnapshot
API将来自viewModel
的数据插入到这两个部分中:
With the above in place, we’ve now built a simple grouped contact list containing two sections, with support for swipe actions and disclosure indicators — all using UICollectionView
.
通过上述操作,我们现在构建了一个简单的分组联系人列表,其中包含两个部分,并支持滑动操作和公开指示器-全部使用UICollectionView
。
The ContactsViewController class should like this:
ContactsViewController类应如下所示:
结论 (Conclusion)
Table views have been a central part of the iOS user interface since the first version of the platform, while collection views were a much later addition. Over the last couple of iOS releases, collection views have become steadily more functional — to the point where in iOS14, UICollectionView
not only become more or less on par with UITableView
in terms of list rendering, it has even surpassed it.
自平台第一个版本以来,表格视图一直是iOS用户界面的核心部分,而集合视图则是后来的新增功能。 在iOS的最后两个版本中,集合视图已变得越来越稳定,在iOS14中, UICollectionView
不仅在列表呈现方面与UITableView
差不多,甚至超过了UITableView
。
So while there still might be edge cases that only UITableView
covers, I think it’s fair to say that UICollectionView
is now the preferred way of building lists on iOS (at least once we’re able to use iOS 14 as an app’s minimum deployment target).
因此,尽管仍然有可能只有UITableView
涵盖一些UICollectionView
情况,但我认为可以说UICollectionView
现在是在iOS上构建列表的首选方式(至少在我们能够将iOS 14用作应用程序的最低部署目标之后) 。
翻译自: https://levelup.gitconnected.com/creating-lists-with-uicollectionview-in-ios-14-89d5fdc880af