与实现 tableview 委托和数据源方法的 UIViewController 相比,使用 UITableViewController 有什么优势吗? [英] Are there any advantages to using a UITableViewController over a UIViewController that implements tableview delegate and datasource methods?

查看:27
本文介绍了与实现 tableview 委托和数据源方法的 UIViewController 相比,使用 UITableViewController 有什么优势吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 中,当我创建一个包含 tableview 的新视图控制器时,我有两个选项

In Xcode when I create a new view controller to contain a tableview I have two options

  1. 创建一个新的UITableViewController
  2. 创建一个新的 UIViewController 来实现 UITableViewDelegateUITableViewDataSource 协议
  1. Create a new UITableViewController
  2. Create a new UIViewController that implements the UITableViewDelegate and UITableViewDataSource 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:

  1. 您需要表视图小于视图控制器的视图.
  2. 您需要向不随表视图滚动的视图控制器添加其他视图(尽管有一些方法可以使用 UITableViewController 解决此问题).
  1. You need the table view to be smaller than the view controller's view.
  2. 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:

  1. 定义和设置UITableView.
  2. 将自身设置为表视图的数据源和委托.
  3. 覆盖 setEditing:animated: 方法以同时设置表视图的 editing 属性.
  4. 根据 clearsSelectionOnViewWillAppear 属性取消选择 viewWillAppear: 方法中最后选定的行.
  5. viewDidAppear: 方法中闪烁表格视图的滚动条.
  6. 连接刷新控件(从 iOS 6 开始).
  7. 第一次出现时重新加载表格视图.
  8. 调整表格视图的contentInset(从 iOS 7 开始).
  9. 当键盘出现时根据需要滚动表格视图.
  1. Defines and setups up the UITableView.
  2. Sets itself as the table view's dataSource and delegate.
  3. Overrides the setEditing:animated: method to also set the editing property of the table view.
  4. Deselects the last selected row in the viewWillAppear: method depending on the clearsSelectionOnViewWillAppear property.
  5. Flashes the table view's scrollbars in the viewDidAppear: method.
  6. Hooks up the refresh control (as of iOS 6).
  7. Reloads the table view the first time it will appear.
  8. Adjusts the table view's contentInset (as of iOS 7).
  9. Scrolls the table view as needed when the keyboard appears.

这篇关于与实现 tableview 委托和数据源方法的 UIViewController 相比,使用 UITableViewController 有什么优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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