UIViewController和UITableViewController之间的区别是什么 [英] What is the difference between UIViewController and UITableViewController

查看:177
本文介绍了UIViewController和UITableViewController之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我想为一些应用程序广泛的自定义子类化UIViewController。例如。所有视图控制器应该在viewDidLoad或viewWillAppear左右执行。

Sometimes I want to subclass UIViewController for some app wide customizations. Eg. something that all view controllers should perform during viewDidLoad or viewWillAppear or so.

当然,我的子类UIViewController,并从那里,所有的视图控制器继承。但是一些控制器运行表。并且有为此目的设计的UITableViewController。

Naturally I subclass UIViewController and go from there and all view controllers inherit from that. But some of the controllers run tables. And there is UITableViewController designed for that purpose.

所以我也子类化UITableViewController,只是做同样的事情。这似乎不是最好的事情在OOP条款。并且没有多重继承等。

So I subclass UITableViewController too and just do the same things there. Which does not seem to be the smartest thing in OOP terms. And there is no multiple inheritance etc.

和UITableViewController继承自UIViewController ...

And as UITableViewController inherits from UIViewController ...

问自己为什么我不是创建自己的表视图控制器继承自我自己的视图控制器子类,并添加所有的表的东西。但什么是所有表的东西

Now I am asking myself why I don't just create my own table view controller that inherits from my very own view controller subclass and adds all the table stuff. But what is "all the table stuff"?


  • xcode添加到每个新表视图控制器的框架代码。相当方便,但可以很容易地移动到代码片段。

  • 有协议UITableViewDelegate和UITableViewDataSource的声明。随和。这些方法的实现必须在UITableViewController的每个子类中。

  • 协议中所有这些强制性方法可能有合理的默认实现。比如为numberOfSectionsInTableView返回0,或者为titleForHeaderInSection返回nil,或者为heightForRowAtIndexPath返回44.0f(很糟糕的例子,可能根本没有实现)。

所以尽管有明显的东西,有没有任何奇迹UITableViewController照顾?

So despite the obvious stuff, are there any miracles that UITableViewController takes care of?

推荐答案

我相信所有的行为 UITableViewController 类文档: https://developer.apple.com/library /ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html

I believe all of the behavior UITableViewController adds is well defined in the class documentation: https://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html


UITableViewController类创建一个控制器对象管理表视图。它实现以下行为:

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

•如果通过initWithNibName:bundle:方法(由超类UIViewController声明)来指定nib文件,UITableViewController将加载表视图归档在nib文件中。否则,它将创建具有正确尺寸和自动调整掩码的未配置UITableView对象。您可以通过tableView属性访问此视图。

• If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.

•如果加载包含表视图的nib文件,数据源和委托将成为nib文件中定义的对象(如果有)。如果没有指定nib文件或者如果nib文件没有定义数据源或委托,UITableViewController会将数据源和表视图的委托设置为self。

• If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

•当表视图即将第一次加载时出现,表视图控制器重新加载表视图的数据。它还会在每次显示表视图时清除其选择(有或没有动画,取决于请求)。 UITableViewController类在超类方法viewWillAppear中实现这一点。您可以通过更改clearsSelectionOnViewWillAppear属性中的值来禁用此行为。

• When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

当表视图出现时,控制器会刷新表视图的滚动指示器。 UITableViewController类在超类方法viewDidAppear中实现这一点:

• When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

它实现了超类方法setEditing:animated:如果用户点击Edit | Done按钮导航栏,控制器切换表的编辑模式。

• It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.

所有这些都是应该很容易重新实现的行为if它们适用于您的特定控制器和表视图。

All of these are behaviors which should be easy to re-implement if they apply to your specific controller and table view.

一般来说,我发现最好自己实现这些行为,以允许替代继承层次结构,我通常考虑设置表视图的 delagate 数据源作为视图控制器的设计气味。这些是独立的关注点,通常可以并且应该由一些其他类来处理(例如,针对特定模型类的专用数据源),而不是膨胀视图控制器。

In general I have found it preferable to implement these behaviors myself in order to allow for alternate inheritance hierarchies as you noted and because I usually consider setting both the delagate and datasource of a table view to be a view controller a design smell. Those are independent concerns which usually can and should be handled by some other class (e.g. a dedicated data source for a particular model class) rather than bloating a view controller.

这篇关于UIViewController和UITableViewController之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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