ios 以编程方式将 UITableView 添加到 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7152974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:12:52  来源:igfitidea点击:

adding a UITableView programmatically to a UIViewController

iphoneioscocoa-touchuikit

提问by Adam

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 加载到我的导航控制器的层次结构之一中,其中将包含一些文本和一些图像。在底部,我想创建一个可展开和可折叠的 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 中吗?

回答by commanda

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 <UITableViewDelegate, UITableViewDataSource> 

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。您最后一次希望 table view 占据整个可用空间是什么时候?通常,除了其他子视图之外,我创建了一个普通的旧 UIViewController 并添加了一个 UITableView 作为其视图的子视图。

回答by 3lvis

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

@interface MyCustomController () <UITableViewDataSource, UITableViewDelegate>
@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

回答by Shane Powell

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

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

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

回答by Rog

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 以及其他阻止你使用 UITableViewController 的自定义 UI 元素时,绝对有可能并且经常使用。

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.

我个人更喜欢在界面构建器中完成整个事情,因为您可以用更少的代码来实现相同的结果。

回答by Dave A

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 的实现上投入大量资金并且觉得重构会很痛苦的人添加这个。