ios - UIViewController 和 UITableViewController 有什么区别

标签 ios cocoa-touch uiviewcontroller uitableview

有时我想子类化 UIViewController 以进行一些应用程序范围的自定义。例如。所有 View Controller 都应该在 viewDidLoad 或 viewWillAppear 期间执行的操作。

自然地,我将 UIViewController 子类化,然后从那里继承所有 View Controller 。但是一些 Controller 运行表。并且有专门为此目的设计的 UITableViewController。

所以我也将 UITableViewController 子类化,并在那里做同样的事情。从 OOP 的角度来看,这似乎不是最聪明的事情。并且没有多重继承等。

并且由于 UITableViewController 继承自 UIViewController ...

现在我问自己为什么不创建自己的 TableView Controller ,它继承 self 自己的 View Controller 子类并添加所有表内容。但什么是“所有表格内容”

  • xcode 添加到每个新的 TableView Controller 的框架代码。非常方便,但可以很容易地移动到代码片段中。
  • 有协议(protocol) UITableViewDelegate 和 UITableViewDataSource 的声明。随和。无论如何,这些方法的实现必须遵循 UITableViewController 的每个子类。
  • 协议(protocol)中所有这些强制性方法可能都有合理的默认实现。例如为 numberOfSectionsInTableView 返回 0:或为 titleForHeaderInSection 返回 nil 或为 heightForRowAtIndexPath 返回 44.0f:(不过是个坏例子。根本不实现它可能更聪明)

因此,尽管有明显的东西,但 UITableViewController 有什么奇迹吗?

最佳答案

我相信 UITableViewController 添加的所有行为都在类文档中得到了很好的定义:https://developer.apple.com/documentation/uikit/uitableviewcontroller?language=objc

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

• 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.

• 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.

• 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.

• 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:.

• 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.

所有这些行为如果适用于您的特定 Controller 和 TableView ,应该很容易重新实现。

总的来说,我发现最好自己实现这些行为,以便如您所述那样允许替代继承层次结构,因为我通常会考虑同时设置 delagatedatasource 一个 TableView 成为一个 View Controller 的设计味道。这些是独立的关注点,通常可以而且应该由其他一些类(例如,特定模型类的专用数据源)处理,而不是膨胀 View Controller 。

关于ios - UIViewController 和 UITableViewController 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17305723/

相关文章:

ios - 我的 iPad 应用程序会终止 wifi 直到最小化

iphone - 使用 addAttribute 修改整个 NSMutableAttributedString :

iphone - 为什么[UIColor白色]和黑色的亮度相等

ios - Twitter Kit 不能在 Swift 应用程序中重定向

IOS 8 NSLocationAlwaysUsageDescription 自定义翻译

objective-c - b/w addObject :dictionary and addObject:[dictionary copy] to an NSMutableArray? 有什么区别

iphone - UILabel文本发光和选框

ios - 无法从主 UIViewController 访问嵌入式 UICollectionView

ios - View Controller 之间的交互转换?

iOS:loadView 的推荐模式