两个uitableviews一个uiviewcontroller

Difficulty: Beginner | Easy | Normal | Challenging

难度: 初学者 | 简单 | 普通| 具有挑战性的

先决条件: (Prerequisites:)

术语 (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

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

代码 (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
Click for Gist 单击要点

viewDidLoad函数 (The viewDidLoad function)

This is then followed up in the viewDidLoad() function by setting up the cell for reuse.

然后,通过设置单元以供重用,在viewDidLoad()函数中进行后续操作。

Image for post
Click for Gist 单击要点

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
Click for Gist 单击要点

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
Click for Gist 单击要点

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值