开发者社区> 橘子红了呐> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

UITableViewController与UIViewController中使用UITableView

简介:
+关注继续查看

之前使用TableView的时候都是继承UIViewController,然后继承两个delegate,如下面的代码。

@interface SomeViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

这篇文章《如何使用UITableView》讲述了我怎样使用TableView。最近想使用iOS6的 UIRefreshControl,不幸的是这个UIRefreshControl 只能使用在UITableViewController里面,不能支持UIViewController。Thanks Apple, make things more difficult. 因此我不对不把UIViewController改成UITableViewController。

这篇文章讲述了两者的区别。

http://www.iosdevnotes.com/tag/uitableviewcontroller/

UIViewController vs UITableViewController

The class that implements the delegate methods is almost always the view controller that owns the table view. What should that class be? Most view controllers are usually subclasses of UIViewController, but iOS also provides a UITableViewController.

UITableViewController only provides a few features on top of UIViewController:

  • UITableViewController has a tableView property built-in that points to its table view.
  • UITableViewController will automatically make itself the data source and delegate, unless you specifically change it.
  • The UITableViewController will reload the table view’s data the first time it’s loaded. It will also clear any selected rows whenever the table view is displayed.
  • After the table view appears, it will flash the table view’s scroll indicators. This is a hint to the user that there’s more data than they currently see on the screen.
  • If there’s a navigation bar with an Edit/Done button, the UITableViewController will hook it up to toggle the edit mode of the table view.

Basically, it saves a little bit of time by automatically implementing some common and expected code. If you don’t want any of this behavior, you can always do it yourself within a UIViewController. Just remember to manually implement the steps listed above yourself, if you still want them. You might have seen apps where you select a row from a table, go to a new screen, and when you come back the row is still highlighted. This is usually a sign that someone used a UIViewController with their table view and forgot to clear the selection :) .

The most common times when I don’t use a UITableViewController in my apps is usually when the view controller needs extra functionality beyond just a table view. Perhaps I want the table view nested inside another view, for example. Usually though, you can use a UITableViewController, as we do in the example below.

A helpful hint: if you’re creating a new view controller though Xcode (like under File -> New -> New File…), you can select “UIViewController subclass”, and then on the next screen, choose “UITableViewController” from the “Subclass of” drop down.

 

 

看起来UITableViewController更加方便,但是我喜欢UIViewController 的灵活性。对于使用UIRefreshControl的需求,我也别无选择,不得不改成UITableViewController。把Storyboard了页面重新做一遍,重新写ViewController类。


    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2013/01/24/UITableViewController_vs_UIViewController_with_UITableView.html,如需转载请自行联系原作者


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
弹出的ViewController半透明效果
在第一个ViewController做如下设置 let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController controller.
583 0
tabbarcontroller切换
<p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo; color:rgb(79,129,135)"> <span style="color:#000000"> </span>MyTabbarViewController<span style="color:#000000"> *tab
920 0
[@Controller]1 基于@注释的控制器配置
[@Controller]1 基于@注释的控制器配置 (2012-06-14 15:20:33)转载▼ 标签: spring mvc controller 注释 控制器 it 分类: JavaSpring 基于注释的控制器配置需要Java 5以上的版本支持。
725 0
UIView UIViewController
--------------------------------------------------------------------------------UIViewController---------------------------...
902 0
3402
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载