以编程方式将UITableView添加到UIViewController [英] adding a UITableView programmatically to a UIViewController

查看:106
本文介绍了以编程方式将UITableView添加到UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个UIViewController到我的Nav控制器的层次结构,它将包含一些文本和一些图像。在底部,我将创建一个可展开和可折叠的表视图。

I'm loading a UIViewController into one of my Nav controller's hierarchies, which will contain some text and some images. At the bottom, I will want to create a expandable and collapsable tableview.

首先,这个想法可能吗?如果是,我该如何添加它,我在哪里放置数据源和委托方法?

First off, is this idea possible? If it is, how do I add it and where do I place the data source and delegate methods?

我可以创建一个单独的TableViewController的子类,然后添加它到我的ViewController作为子视图?

Can I just make a separate subclass of the TableViewController and then add it to my ViewController as a subview?

推荐答案

是的,你可以创建一个UITableView的委托,数据源和父视图不一定一个UITableViewController。因为UITableView是一个UIView,你可以添加它作为任何其他UIView的子视图。任何NSObject可以是委托或数据源,只要你实现所需的协议方法。

Yes, you can create a UITableView whose delegate, datasource, and parent view are not necessarily a UITableViewController. Since the UITableView is a UIView, you can add it as a subview of any other UIView. Any NSObject can be the delegate or datasource, as long as you implement the required protocol methods.

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

事实上,根据我的经验,没有很多人甚至使用UITableViewControllers。什么时候是你最后一次想要你的表视图占用整个可用空间?一般来说,我创建一个普通的旧UIViewController,并添加一个UITableView作为其视图的子视图,除了其他子视图。

In fact, in my experience, not many people even use UITableViewControllers. When was the last time you wanted your table view to take up the entire usable space? In general, I create a plain old UIViewController and add a UITableView as a subview of its view, in addition to other subviews.

这篇关于以编程方式将UITableView添加到UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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