一个UITableView - 多个DataSource,最好的设计模式? [英] One UITableView - Multiple DataSource, best design pattern?

查看:171
本文介绍了一个UITableView - 多个DataSource,最好的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个典型的问题,但我有一个UITableView具有相同的行为两个单独的数据源。什么是最好的方式去设计类层次结构有尽可能少的重复和if / else条件?视图控制器将对两个数据源执行相同的操作,它们在数据集中是唯一的。我应该让父控制器只设置它的数据源/相应的标题?

This seems like a typical problem, but I have a UITableView that has identical behavior for two separate data sources. What is the best way of going about designing the class hierarchy to have as little duplication and if/else conditions? The view controller is going to do the same exact thing to both data sources, they're just unique in their data set. Should I have the parent controller just set its data source/respective title?

当使用UISegmentControl显示具有相同接口的两个视图时,同样的问题也是相关的,但不同的数据源。

The same issue is relevant as well when using a UISegmentControl for displaying two views with the same interfaces, but different data sources.

推荐答案

在这里小心你的术语。 UITableView有一个名为 dataSource 的东西,但你似乎基本上是指两组不同的数据。

Be careful with your terminology here. A UITableView has something called a dataSource but you seem to be referring, essentially, to two different sets of data.

在你建议的情况下,在表的 dataSource (遵守UITableViewDataSource协议的对象),我有三个数组。

In the case you're suggesting, in the table's dataSource (the object that adheres to the UITableViewDataSource protocol), I'd have three arrays.


  • currentlyViewedArray

  • datasetOneArray

  • datasetTwoArray

在dataSource方法中,使用currentlyViewedArray作为表数据的源。

In the dataSource methods, use the currentlyViewedArray as the source of the table's data.

然后,设置currentlyViewedArray到您要查看的数组:

Then, set the currentlyViewedArray to whichever array you want to view:

self.currentlyViewedArray = self.datasetOneArray;
[theTableView reloadData];

您可以使用UISegmentedControl在两个数组之间切换。

You can use the UISegmentedControl to switch between the two arrays.

这篇关于一个UITableView - 多个DataSource,最好的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆