热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

以编程方式将UITableView添加到UIViewController-addingaUITableViewprogrammaticallytoaUIViewController

ImloadingaUIViewControllerintooneofmyNavcontrollershierarchies,whichwillcontainsome

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.

我正在将一个UIViewController加载到我的一个Nav控制器层次结构中,它将包含一些文本和一些图像。在底部,我将要创建一个可扩展和可折叠的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?

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

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

我可以只创建一个TableViewController的子类,然后将其作为子视图添加到我的ViewController中吗?

5 个解决方案

#1


20  

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.

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

@interface MyViewController : UIViewController  

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.

事实上,根据我的经验,甚至没有多少人使用UITableViewControllers。您最后一次希望表视图占用整个可用空间的时间是什么时候?通常,我创建一个普通的旧UIViewController,并添加一个UITableView作为其视图的子视图,以及其他子视图。

#2


15  

/************************************************/
/************* MyCustomController.m *************/
/************************************************/

@interface MyCustomController () 
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MyCustomController

- (id)initWithNibName:(NSString*)nibName bundle:(NSString*)bundleName
{
   self = [super initWitNibName:nibName bundle:bundleName];
   if (self)
   {
       self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
       tableView.datasource = self; 
       tableView.delegate = self;
       [self.view addSubview:self.tableView];
   }

   return self;
}

#pragma mark - UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return number of rows
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // return cell
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // handle table view selection
}

@end

#3


11  

It's pretty easy, in something like your viewDidLoad method:

它很简单,就像你的viewDidLoad方法:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];

#4


4  

Just remember that a UITableViewController is a subclass of UIViewController only with the tableview set as the controller's view.

请记住,UITableViewController只是UIViewController的子类,只有tableview设置为控制器的视图。

So yes definitely possible and used quite frequently when you want to have a tableview but also other custom UI elements which prevent you from using the UITableViewController.

因此,当您想要一个tableview以及其他自定义UI元素阻止您使用UITableViewController时,肯定可以并且经常使用。

I'd normally choose to add it to my view controller's view in either its initialisation method or viewDidLoad method. This will vary based on whether you're creating your views from a NIB or entirely programatically.

我通常会选择在初始化方法或viewDidLoad方法中将它添加到我的视图控制器视图中。这取决于您是从NIB创建视图还是以编程方式创建视图。

In case of NIBs:

如果是NIB:

- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)bundleName
{
   if ((self = [super initWitNibName:nibName bundle:bundleName]))
   {
       self.theTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewWhateverStyleYouWantHere];
       theTableView.dataSource = self, theTableView.delegate = self;
       [self.view addSubview:theTableView];
       [theTableView release];
   }
}

And then you can set the frame of your tableview in your viewDidLoad method.

然后,您可以在viewDidLoad方法中设置tableview的框架。

I'd personally prefer to do the whole thing in interface builder as you'd achieve the same result with way less code to maintain.

我个人更喜欢在界面构建器中完成所有操作,因为您可以通过减少维护代码来实现相同的结果。

#5


2  

If you're like me and already had created a UITableViewController and then realizing that you did so much work on it that re-writing it would be a pain, you can just do the following to add the UITableViewController to the UIViewController as a subview.

如果你像我一样已经创建了一个UITableViewController,然后意识到你做了很多工作,重写它将是一件痛苦的事,你只需要执行以下操作即可将UITableViewController作为子视图添加到UIViewController中。

UITableViewController* tableViewCOntroller= [[UITableViewController alloc] init];
[self.view addSubview:tableViewController.tableView];

All the other answers above works great. I figure I'd add to this for those that have a heavily invested implementation of a UITableViewController and feel like refactoring would be a pain.

上面的所有其他答案都很有效。我想我会为那些有大量投资实现UITableViewController的人添加这个,并且觉得重构会很痛苦。


推荐阅读
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • Kubernetes 持久化存储与数据卷详解
    本文深入探讨 Kubernetes 中持久化存储的使用场景、PV/PVC/StorageClass 的基本操作及其实现原理,旨在帮助读者理解如何高效管理容器化应用的数据持久化需求。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文探讨了如何在编程中正确处理包含空数组的 JSON 对象,提供了详细的代码示例和解决方案。 ... [详细]
  • Ralph的Kubernetes进阶之旅:集群架构与对象解析
    本文深入探讨了Kubernetes集群的架构和核心对象,详细介绍了Pod、Service、Volume等基本组件,以及更高层次的抽象如Deployment、StatefulSet等,帮助读者全面理解Kubernetes的工作原理。 ... [详细]
  • 深入理解Shell脚本编程
    本文详细介绍了Shell脚本编程的基础概念、语法结构及其在操作系统中的应用。通过具体的示例代码,帮助读者掌握如何编写和执行Shell脚本。 ... [详细]
  • 本文介绍了如何在iOS应用中自定义导航栏按钮,包括使用普通按钮和图片生成导航条专用按钮的方法。同时,探讨了在不同版本的iOS系统中实现多按钮布局的技术方案。 ... [详细]
author-avatar
遗忘不能忘
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有