在实现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 $ c $是否有任何优势(除了不必编写方法存根) C>?意味着,在
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
。 - 将自身设置为表视图的dataSource和delegate。
- 覆盖
setEditing:animated:
方法,同时设置编辑
属性表视图。 - 取消选择
viewWillAppear:
方法中的最后一个选定行,具体取决于clearsSelectionOnViewWillAppear
property。 - 在
viewDidAppear:
方法中闪烁表格视图的滚动条。 - 连接刷新控件(从iOS 6开始)。
- 第一次出现时重新加载表格视图。
- 调整表格view的
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屋!