Difficulty: Beginner | Easy | Normal | Challenging
难度: 初学者 | 简单 | 普通| 具有挑战性的
先决条件: (Prerequisites:)
You must be able to create a Single View Application, although you can complete the same in Playgrounds. You’ll need to know about IF statements. Knowledge of how to create a UITableView instance would be useful, but I’ll take you through the basics in this tutorial
尽管您可以在Playgrounds中完成相同的操作,但是您必须能够创建Single View Application 。 您需要了解IF语句 。 了解如何创建UITableView实例的知识将很有用,但我将带您了解本教程的基础知识
术语 (Terminology)
UITableView: A view that presents data using rows arranged in a single column
UITableView:一种视图,该视图使用排列在单列中的行来显示数据
View controller: Sits between the view and the model, tying them together (usually using the delegate pattern). The controller is not tightly bound to a concrete view, and communicates via a protocol to an abstraction. An example of this is the way that a UITableView communicates with its data source through the UITableViewDataSource protocol. Think of it as the how of the App. The primary job of the controller is to format the data from the model for the view to display.
视图控制器:位于视图和模型之间,将它们绑在一起(通常使用委托模式)。 控制器未紧密绑定到具体视图,而是通过协议与抽象进行通信。 一个示例就是UITableView通过UITableViewDataSource协议与其数据源进行通信的方式。 将其视为应用程序的方式。 控制器的主要工作是格式化模型中的数据以供视图显示。
挑战 (The challenge)
What would happen if I were to whack two UITableView
instances into a single view controller?
如果我将两个UITableView
实例UITableView
到一个视图控制器中会发生什么?
How might both instances communicate with the dataSource and the delegate?
两个实例如何与数据源和委托进行通信?
Certainly the solution would look like the following:
当然,解决方案如下所示:
![Image for post](https://miro.medium.com/max/9999/1*4TvHOVH9KYrg8pF3vN_H0A.png)
Let us see how we can put this into action!
让我们看看如何将其付诸实践!
设置情节提要 (Setting up the storyboard)
You could also attempt this programatically (or however you want to, I don’t even know you so go wild).
您也可以以编程方式尝试执行此操作(或者,但是您想这样做,我什至都不知道您会如此疯狂)。
In my case, I added a stack view to keep everything tidy, and spaced two tableview instances equally with a spacing of 8 between them. You can see the completed view amongst the completed project @The repo link.
就我而言,我添加了一个堆栈视图以使所有内容保持整洁,并将两个tableview实例平均间隔为8。 您可以在已完成的项目@The repo链接中看到已完成的视图。
![Image for post](https://miro.medium.com/max/9999/1*hgqBuHZUyMo-2jul8ZN-eA.png)
代码 (The code)
网点和数据 (The outlets and data)
Within the brilliantly named UIViewController
ViewController we set up a couple of outlets for the UITableView
instances, and prepare some (creative) data for them to display.
在名称精美的UIViewController
ViewController中,我们为UITableView
实例设置了两个出口,并准备了一些(创意)数据供其显示。
![Image for post](https://img-blog.csdnimg.cn/img_convert/60589b720183b54b80152ea698c612ff.png)
viewDidLoad函数 (The viewDidLoad function)
This is then followed up in the viewDidLoad()
function by setting up the cell for reuse.
然后,通过设置单元以供重用,在viewDidLoad()
函数中进行后续操作。
![Image for post](https://img-blog.csdnimg.cn/img_convert/6abb51263d96819eed1dc2d3189a33b3.png)
We can see that the delegate and the data source for each UITableView
instance has been set to the same viewController. Don’t worry — this will be dealt with in the following code.
我们可以看到每个UITableView
实例的委托和数据源已设置为相同的viewController。 不用担心-这将在以下代码中处理。
UITableViewDataSource (The UITableViewDataSource)
The minimal implementation of UITableViewDataSource
implements tableView(_:numberOfRowsInSection:)
and tableView(_:cellForRowAt:)
. The question is which tableView is the function called from? It turns out that this is called by both, however one of the function parameters is actually the tableview.
UITableViewDataSource
的最小实现是实现tableView(_:numberOfRowsInSection:)
和tableView(_:cellForRowAt:)
。 问题是函数从哪个tableView调用? 事实证明,两者均调用了此函数,但是函数参数之一实际上是tableview。
Now the example uses a simple if statement (although any switch or branching method would be suitable here) to behave with the appropriate UITableView
instance.
现在,该示例使用一个简单的if语句(尽管此处适合使用任何开关或分支方法)来与适当的UITableView
实例配合使用。
![Image for post](https://miro.medium.com/max/9999/1*85liYJRKHB5zya_gIo9obQ.png)
Therefore the correct cell is returned depending on the UITableView
appropriate to the function call.
因此,根据适合该函数调用的UITableView
返回正确的单元格。
A similar exercise can take place using the UITableViewDelegate
.
使用UITableViewDelegate
可以进行类似的练习。
![Image for post](https://img-blog.csdnimg.cn/img_convert/1092bc4daadebcaaa784914027faf652.png)
结论 (Conclusion)
Creating a table view is really important when creating Apps using the iOS SDK.
使用iOS SDK创建应用程序时,创建表格视图非常重要。
Putting two such objects on the same Storyboard might seem like something that you don’t do frequently, however it is also something that you should know how to do as you might be able to think of some interesting uses for this skill.
将两个这样的对象放在同一个Storyboard上似乎并不常用,但是您也应该知道该怎么做,因为您可能会想到此技能的一些有趣用途。
Have fun coding!
祝您编码愉快!
I’d love to hear from you if you have questions
如果您有任何疑问,我很想听听您的意见
翻译自: https://medium.com/@stevenpcurtis.sc/two-uitableviews-one-uiviewcontroller-a98181246164