博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableViewController与UIViewController中使用UITableView
阅读量:6828 次
发布时间:2019-06-26

本文共 2651 字,大约阅读时间需要 8 分钟。

 

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

@interface SomeViewController : UIViewController 

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

转载地址:http://qzykl.baihongyu.com/

你可能感兴趣的文章
Lintcode: Add Binary
查看>>
人大、上财、复旦、上交四校2013年应届金融硕士就业去向
查看>>
技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)
查看>>
[转]【HTTP】Fiddler(二) - 使用Fiddler做抓包分析
查看>>
Cts框架解析(8)-IBuildProvider
查看>>
Tomcat 项目部署方式
查看>>
微软收购Xamarin,你怎么看?
查看>>
[caffe]深度学习之图像分类模型AlexNet解读
查看>>
HTTPS科普扫盲帖
查看>>
[na]那些OVER的封装(pppoe/ppp/ipsec)
查看>>
C# 导出Excel的示例(转载)
查看>>
python环境搭建,开发环境
查看>>
asp.net mvc 之旅—— 第三站 路由模板中强大的自定义IRouteConstraint约束
查看>>
[TypeScript] Understanding Decorators
查看>>
解决Matlab画图直接保存.eps格式而导致图不全的问题
查看>>
把C#程序(含多个Dll)合并打包成单一文件
查看>>
BZOJ 3339: Rmq Problem 莫队算法
查看>>
iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
查看>>
Ubuntu中给eclipse和android studio添加桌面快捷图标
查看>>
find-all-duplicates-in-an-array(典型的数组中的重复数,不错,我做出来了,可是发现别人有更好的做法)...
查看>>