使用LazyVStack的SwiftUI中的TableViews

At WWDC 2020, Apple introduced the LazyVStack. In my opinion, it is an improved version of VStack due to its advantage in performance.

在WWDC 2020上,苹果推出了LazyVStack 。 我认为,由于其性能优势,它是VStack的改进版本。

LazyVStack’s behaviour is similar to VStack’s with the exception of laziness. In many instances, “lazy” means if you do not see it on your screen, it is not being loaded. This, in turn, increases performances. On the other hand, VStack loads everything.

除了懒惰之外, LazyVStack的行为与VStack相似。 在许多情况下,“惰性”是指如果您在屏幕上看不到它,则表示它没有被加载。 反过来,这可以提高性能。 另一方面, VStack会加载所有内容。

Just think of it this way: If you have a huge load of data, the ideal approach is to load only what you see. This is what LazyVStack does. As you scroll, you will load the data.

只是这样想:如果您有大量数据,理想的方法是只加载您看到的内容。 这就是LazyVStack所做的。 滚动时,将加载数据。

In summary, use LazyVStack if you have a huge amount of data and VStack when you have a relatively small amount of data.

总之,使用LazyVStack如果你有一个庞大的数据和量VStack当你有数据相对较少。

“A view that arranges its children in a line that grows vertically, creating items only as needed.” — Apple Developer

“将子项排列在垂直方向上的视图,仅在需要时创建项目。” - 苹果开发商

先决条件 (Prerequisites)

To follow along with this tutorial, you’ll need some basic knowledge in:

要继续学习本教程,您将需要一些基本知识:

  • Swift

    Swift
  • At least Xcode 12+

    至少Xcode 12+

Note: This only supports iOS 14+.

注意:这仅支持iOS 14+。

LazyVStack入门 (Getting Started With LazyVStack)

The usage is similar to VStack’s, and I took this a little further and made it more interesting when playing with LazyVStack.

用法与VStack相似,我对此进行了进一步介绍,使其在使用LazyVStack时更加有趣。

This is the scenario: It’s now 2024 and you were recently hired by a company to build an employee database. The minimum iOS on each iPhone device is at least iOS16+ and the requirement for this is that it shouldn’t be laggy. You instantly think of LazyVStack. Bear in mind that the minimum iOS requirement is iOS14+.

场景是这样:现在是2024年,您最近被一家公司雇用来建立员工数据库。 每个iPhone设备上的最小iOS至少为iOS16 +,对此的要求是它不宜过时。 您立刻想到了LazyVStack 。 请记住,最低iOS要求是iOS14 +。

Now you are getting ready to get your hands dirty and start coding. To make it look real, I just wrote a code that randomly generated some ID:

现在,您可以开始动手并开始编码了。 为了使它看起来真实,我只是编写了一个随机生成一些ID的代码:

Image for post

滚动LazyVStack (Scrolling LazyVStack)

Almost done! The only problem is it’s not scrolling. That should be an easy fix. Wrap it inside ScrollView:

快完成了! 唯一的问题是它没有滚动。 那应该很容易解决。 将其包装在ScrollView

ScrollView {
...
...
}

Now let’s see the scrolling effect. Doesn’t this remind you of UITableView? The only differences are shorter code and no need to use the delegate and datasource.

现在让我们看一下滚动效果。 这不是让您想起UITableView吗? 唯一的区别是较短的代码,无需使用委托和数据源。

Image for post

It’s only taken five minutes. Since you are getting paid hourly, you want to make this look better with more customisation.

只需要五分钟。 由于您是按小时收取工资的,因此您希望通过更多的自定义来使其看起来更好。

LazyVStack定制 (LazyVStack Customisation)

What about adding a header? It should look more like a UITableView now.

如何添加标题? 现在,它看起来应该更像一个UITableView

Add an extra parameter called pinnedViews and indicate that you would like a header by including .sectionHeaders as the parameter value. You may even add a footer with .sectionFooters:

添加一个名为pinnedViews的额外参数,并通过将.sectionHeaders作为参数值来指示您想要一个标头。 您甚至可以使用.sectionFooters添加页脚:

LazyVStack(alignment: .leading, pinnedViews: [.sectionHeaders]) {
...

With that, we will now have to wrap everything inside the Section:

这样,我们现在必须将所有内容包装在Section

LazyVStack(alignment: .leading, pinnedViews: [.sectionHeaders]) {
Section(header: Text("Company XYZ").padding()) {
ForEach(1...100, id: \.self) { value in
...
...

The only problem is the background colour is .clear and there is much flexibility for customisation here:

唯一的问题是背景颜色是.clear并且在此处进行自定义具有很大的灵活性:

Image for post

The workaround is to create your view and customise everything inside the view:

解决方法是创建视图并自定义视图中的所有内容:

Now use the newly created HeaderView as the header:

现在,使用新创建的HeaderView作为标题:

Section(header: HeaderView()) {
...

You should now see a more appealing header compared to the previous one:

与上一个相比,您现在应该看到一个更具吸引力的标题:

Image for post

Well done. I am sure your company will be very pleased with the result.

做得好。 我相信您的公司会对结果感到非常满意。

翻译自: https://medium.com/better-programming/tableviews-in-swiftui-with-lazyvstack-8de9d8bc17ee

【课程特点】1、231节大容量课程:包含了SwiftUI的大部分知识点,详细讲解SwiftUI的方方面面;2、15个超级精彩的实例:包含美食、理财、健身、教育、电子商务等各行业的App实例;3、创新的教学模式:手把手教您SwiftUI用户界面开发技术,一看就懂,一学就会;4、贴心的操作提示:让您的眼睛始终处于操作的焦点位置,不用再满屏找光标;5、语言简洁精练:瞄准问题的核心所在,减少对思维的干扰,并节省您宝贵的时间;6、视频短小精悍:即方便于您的学习和记忆,也方便日后对功能的检索;7、齐全的学习资料:提供所有课程的源码,在Xcode 11 + iOS 13环境下测试通过; 更好的应用,更少的代码!SwiftUI是苹果主推的下一代用户界面搭建技术,具有声明式语法、实时生成界面预览等特性,可以为苹果手机、苹果平板、苹果电脑、苹果电视、苹果手表五个平台搭建统一的用户界面。SwiftUI是一种创新、简单的iOS开发的界面布局方案,可以通过Swift语言的强大功能,在所有的Apple平台上快速构建用户界面。 仅使用一组工具和API为任何Apple设备构建用户界面。SwiftUI具有易于阅读和自然编写的声明式Swift语法,可与新的Xcode设计工具无缝协作,使您的代码和设计**同步。自动支持动态类型、暗黑模式、本地化和可访问性,意味着您的**行SwiftUI代码已经是您编写过的非常强大的UI代码了。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值