与实现 tableview 委托和数据源方法的 UIViewController 相比,使用 UITableViewController 有什么优势吗? [英] Are there any advantages to using a UITableViewController over a UIViewController that implements tableview delegate and datasource methods?
问题描述
在 Xcode
中,当我创建一个包含 tableview 的新视图控制器时,我有两个选项
In Xcode
when I create a new view controller to contain a tableview I have two options
- 创建一个新的
UITableViewController
- 创建一个新的
UIViewController
来实现UITableViewDelegate
和UITableViewDataSource
协议
- Create a new
UITableViewController
- Create a new
UIViewController
that implements theUITableViewDelegate
andUITableViewDataSource
protocols
假设我正确实现了协议的所有必需和可选方法,使用 UITableViewController
是否有任何优势(除了不必编写方法存根)?意味着,是否在 UITableViewController
类的幕后实现了什么(内存管理、缓存等)使选项 1 比选项 2 更好?
Assuming I properly implement all of the required and optional methods for the protocols, is there any advantage (besides not having to write the method stubs) to using the UITableViewController
? means, is there anything (memory management, caching, etc.) implemented behind the scenes in the UITableViewController
class that makes option 1 a better choice than option 2?
推荐答案
无论采用两种方法中的哪一种,都必须编写委托和协议方法.
You have to write the delegate and protocol methods regardless of which of the two approaches you take.
当你需要一个带有表格视图的视图控制器时,你应该选择使用 UIViewController
而不是 UITableViewController
只有两个可能的原因:
There are only two possible reasons you should choose to use UIViewController
over UITableViewController
when you need a view controller with a table view:
- 您需要表视图小于视图控制器的视图.
- 您需要向不随表视图滚动的视图控制器添加其他视图(尽管有一些方法可以使用
UITableViewController
解决此问题).
- You need the table view to be smaller than the view controller's view.
- You need to add additional views to the view controller that don't scroll with the table view (though there are ways to solve this with
UITableViewController
).
以下是 UITableViewController
为你做的所有你需要复制的事情:
Here are all of the things that UITableViewController
does for you that you would need to replicate:
- 定义和设置
UITableView
. - 将自身设置为表视图的数据源和委托.
- 覆盖
setEditing:animated:
方法以同时设置表视图的editing
属性. - 根据
clearsSelectionOnViewWillAppear
属性取消选择viewWillAppear:
方法中最后选定的行. - 在
viewDidAppear:
方法中闪烁表格视图的滚动条. - 连接刷新控件(从 iOS 6 开始).
- 第一次出现时重新加载表格视图.
- 调整表格视图的
contentInset
(从 iOS 7 开始). - 当键盘出现时根据需要滚动表格视图.
- Defines and setups up the
UITableView
. - Sets itself as the table view's dataSource and delegate.
- Overrides the
setEditing:animated:
method to also set theediting
property of the table view. - Deselects the last selected row in the
viewWillAppear:
method depending on theclearsSelectionOnViewWillAppear
property. - Flashes the table view's scrollbars in the
viewDidAppear:
method. - Hooks up the refresh control (as of iOS 6).
- Reloads the table view the first time it will appear.
- Adjusts the table view's
contentInset
(as of iOS 7). - Scrolls the table view as needed when the keyboard appears.
这篇关于与实现 tableview 委托和数据源方法的 UIViewController 相比,使用 UITableViewController 有什么优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!