热门标签 | 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的人添加这个,并且觉得重构会很痛苦。


推荐阅读
  • 提升Python编程效率的十点建议
    本文介绍了提升Python编程效率的十点建议,包括不使用分号、选择合适的代码编辑器、遵循Python代码规范等。这些建议可以帮助开发者节省时间,提高编程效率。同时,还提供了相关参考链接供读者深入学习。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 浏览器中的异常检测算法及其在深度学习中的应用
    本文介绍了在浏览器中进行异常检测的算法,包括统计学方法和机器学习方法,并探讨了异常检测在深度学习中的应用。异常检测在金融领域的信用卡欺诈、企业安全领域的非法入侵、IT运维中的设备维护时间点预测等方面具有广泛的应用。通过使用TensorFlow.js进行异常检测,可以实现对单变量和多变量异常的检测。统计学方法通过估计数据的分布概率来计算数据点的异常概率,而机器学习方法则通过训练数据来建立异常检测模型。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
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社区 版权所有