在iOS 14中使用UICollectionView创建列表

本文介绍了在iOS 14中如何利用UICollectionView创建列表,包括设置、UICollectionLayoutListConfiguration、单元配置和注册、构造和更新可扩散数据源。通过新的API,可以实现类似UITableView的功能,如滑动操作和披露指示器。
摘要由CSDN通过智能技术生成

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.listUICollectionViewListCofiguration的引入, 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配对UICollectionViewListCellUICollectionView现在支持以前仅可用于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

Image for post

The collection view’s sections are modelled using an enum Section:

集合视图的部分使用enum Section建模:

Image for post

In the view controller ContactsViewController, create three properties — a view model, that contains the data, a UICollectionView and a UICollectionViewDataSourceto render the data.

在视图控制器ContactsViewController中,创建三个属性-一个包含数据的视图模型,一个UICollectionView和一个UICollectionViewDataSource来呈现数据。

Image for post

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,您可以通过组合或合成较小的构建基块来构建各种布局。 布局可以为共同的列表中很容易地创建为变体, plaingroupedinsetGrouped ,这使我们能够快速配置列表类似的集合视图。

Image for post

单元配置和注册 (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 UICollectionViewCellsubclass 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 ,我们可以将数据绑定到它,而不必知道其确切的子视图。

Image for post

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来呈现我们的联系人列表的各个单元格:

Image for post

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实例,如下所示:

Image for post

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的数据插入到这两个部分中:

Image for post
Image for post

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类应如下所示:

Image for post

结论 (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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值