Objective-C / iOS:将UITableViewController子类化为自定义视图 [英] Objective-C / iOS: Subclassing UITableViewController for a custom view
问题描述
众所周知,Cocoa Touch中的表格视图是其中最精致的框架元素之一。为方便起见,Apple为我们提供了一个不错的视图控制器类,用于将表视图的功能封装在vc中,即 UITableViewController
。
As we all know, table views in Cocoa Touch are one of the niftiest pieces of framework elements that's out there. As a convenience, Apple gave us a nice view controller class to encapsulate the functionality of a table view in a vc, the UITableViewController
.
同时,有时候我们想利用表格视图的功能而不占用整个屏幕。但是,似乎没有办法通过将 UITableViewController
子类化。相反,我必须连接一个表视图并手动订阅 UITableViewDelegate
和 UITableViewDataSource
。如果我尝试对 UITableViewController
进行子类化,则我的应用程序崩溃之前甚至还没有将视图显示在屏幕上……
At the same time, there are times that we want to utilize the functionality of a table view without having it take up the whole screen. However, there seems to be no way to do this by subclassing UITableViewController
. Instead, I had to hookup a table view and manually subscribe to the UITableViewDelegate
and UITableViewDataSource
. If I try to subclass UITableViewController
, my app crashes before it can even put the view on-screen...
我的问题是,我缺少什么吗?当子类化 UITableViewController
时,我将自定义表视图连接到 UITableViewController
中的tableView属性。
My question is, is there something I'm missing? When subclassing UITableViewController
, I hook up my custom table view to the tableView property in UITableViewController
. Is there something else I have to do?
推荐答案
UITableViewController
仅添加了什么? UIViewController
的一些小便利:它创建并放置表格视图,并连接委托&数据源(通常本身)将视图控制器的 editing
属性传递到表中,并在视图出现时执行几个有用的UI位。 (有关详细信息,请参见[docs] [1]。)
UITableViewController
only adds minor conveniences over UIViewController
: it creates and positions the table view, hooks up the delegate & datasource (to itself, generally), passes the view controller editing
property through to the table, and does a couple of useful UI bits when the view appears. (See [the docs][1] for details.)
上述所有内容都是A)您需要更改的内容非全屏表格,或B)您可以一行或两行完成的事情,而 UITableViewController
只是为了您的方便。对于这种情况,最好使用自己的 UIViewController
子类。
Just about all of the above are either A) things that you're needing to change in order to have a non-fullscreen table, or B) things that you can do in a line or two each, and which UITableViewController
only does for your convenience. For cases like this, you're better off using your own UIViewController
subclass.
这篇关于Objective-C / iOS:将UITableViewController子类化为自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!