简体   繁体   English

UITabBarController中的“重用” UITableViewController

[英]“Reuse” UITableViewController in UITabBarController

So I started to work on an app, it has 5 tabs of which 3 shows UITableViewController with custom cells, but with different data. 因此,我开始研究一个应用程序,它有5个选项卡,其中3个显示的UITableViewController具有自定义单元格,但数据不同。

Is it possible to link 3 tabs to same UITableViewController, but check which tab is selected, and according to that, load right data? 是否可以将3个选项卡链接到相同的UITableViewController,但是检查选择了哪个选项卡,并根据此选项加载正确的数据?

I tried putting in UITableViewController: 我尝试放入UITableViewController:

if (self.tabBarController.selectedIndex == 0)
//Load array1
else if (self.tabBarController.selectedIndex == 1)
//Load array2
else
//Load array3

But the indexes are not always the same (it depends in which order you select tabs?, and sometimes I get index that is very high number). 但是索引并不总是相同的(它取决于您选择标签的顺序?有时我会得到非常高的索引)。

How would you do this? 你会怎么做?

Assuming you mean "Is it possible to link 3 tabs to same UITableViewController instance ": 假设您的意思是“是否可以将3个选项卡链接到相同的UITableViewController 实例 ”:

It is possible, but it's not a great idea. 有可能,但这不是一个好主意。 I believe (but couldn't point you at chapter and verse) that the assumption by Apple is that different tabs will have unique, different view controller instances in them. 我相信(但不能在章节中指出您)苹果公司的假设是不同的选项卡中将具有唯一的,不同的视图控制器实例。 When you change tabs, the view controller instance about to hide has various 'about to be hidden' lifecycle methods called on it. 更改选项卡时,要隐藏的视图控制器实例具有各种“即将被隐藏”的生命周期方法。 Likewise, the view controller instance about to appear has various 'about to be shown' lifecycle methods shown on it. 同样,即将出现的视图控制器实例具有在其上显示的各种“即将显示”生命周期方法。 It's possible your single view controller instance may fall foul of the order of these lifecycle methods being shown. 您的单个视图控制器实例可能会与所显示的这些生命周期方法的顺序发生冲突。

The above point aside, it's also a waste of effort reloading a table each time just because the user changed a tab. 撇开上述观点,每次仅由于用户更改了选项卡就浪费了重新加载表的工作量。 Be kind and do things the way you're meant to do them. 善待他人,以应有的方式做事。 There's no reason you can't have a single view controller class that handles everything, but you create a different instance of it for each tab. 没有理由没有一个单一的视图控制器类来处理所有事情,但是您为每个选项卡创建了一个不同的实例。

Note: your UIViewController being aware of which tab it is in counts as a 'code smell'. 注意:您的UIViewController知道它位于哪个标签中才算是“代码异味”。 This usually means you're doing something in an undesirable fashion and hurting the re-usability of this code. 这通常意味着您以不良的方式进行操作,并且损害了此代码的可重用性。 That applies in this case. 在这种情况下适用。

To avoid this 'code smell', your view controller should be able to be given the data it is to show, without any knowledge of even being shown in a UITabBarController. 为了避免这种“代码异味”,您的视图控制器应该能够获得要显示的数据,而无需知道在UITabBarController中是否被显示。

Leverage the UITabBarControllerDelegate to detect when the user switches tabs. 利用UITabBarControllerDelegate来检测用户何时切换选项卡。

Upon tab switch: 在选项卡切换时:

  • assign the new array contents to a member variable used to display the data in the table view (with a similar if/else block as you posted), and 将新的数组内容分配给用于在表视图中显示数据的成员变量(具有与您发布的类似的if / else块),以及
  • call reloadData on your UITableView to trigger a redraw with the data 在UITableView上调用reloadData以触​​发数据重绘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 uitabbarcontroller + uitableviewcontroller + uiviewcontroller - uitabbarcontroller + uitableviewcontroller + uiviewcontroller 打开UITableViewController作为UItabbarcontroller的一项 - open UITableViewController as an item of UItabbarcontroller UITabBarController中的ABPeoplePickerNavigationController和UITableViewController - ABPeoplePickerNavigationController and UITableViewController in a UITabBarController 无法将UITapGestureRecognizer添加到UITabbarController内的UITableViewController - Can't add UITapGestureRecognizer to UITableViewController Inside a UITabbarController 带有 UITabBarController 的 UITableViewController 不显示 - UITableViewController withing UITabBarController does not show up 在xcode 4.3中使用UITableViewController xib创建UITabbarController - create UITabbarController with UITableViewController xib in xcode 4.3 当单击单元格时,查看触发但不推送/ UITabBarController / UITableViewController - Segue firing but not pushing / UITabBarController / UITableViewController when cell clicked 无法让辅助UITableViewController显示在UITabBarController内部 - Can't get Secondary UITableViewController to display inside a UITabBarController uitabbar控制器 - uitabbarcontroller 如何在iPhone中使用按钮上的导航栏在UITabBarController屏幕中调用UITableViewController? - How i calling UITableViewController in UITabBarController screen using Navigation bar on button click in iphone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM