简体   繁体   English

如何将静态UITableView设置为UIView的子视图?

[英]How to setup a static UITableView as a subview of UIView?

When I work with a TableViewController I am able to setup all my content in storyboards. 当我使用TableViewController我能够在故事板中设置我的所有内容。 Since I use Static Cells instead of Dynamic Properties for my table view, I find this method much more convenient and easier to implement. 由于我在表格视图中使用静态单元格而不是动态属性 ,因此我觉得这种方法更方便,更容易实现。 I hook up the new UITableView class and simply delete all the delegate methods. 我挂钩新的UITableView类,只是删除所有委托方法。 Works like a charm as ALL of the content / buttons are being setup in storyboards. 像故事一样工作,因为所有内容/按钮都在故事板中设置。

I am trying to accomplish the same result, except this time, I have to work within a ViewController and add a TableView as a subview. 我试图完成相同的结果,除了这次,我必须在ViewController工作并添加一个TableView作为子视图。 Once I hook up the right class, add my outlet connection and setup the following delegates: 一旦我连接了正确的类,添加我的插座连接并设置以下代理:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

This works well if my TableView is set to Dynamic Properties : 如果我的TableView设置为动态属性,这很有效 在此输入图像描述

But when I change the Table View content to Static Cells and delete the delegate method, my app crashes. 但是,当我将表格视图内容更改为静态单元格并删除委托方法时,我的应用程序崩溃了。 So, How can I add a table view with Static Cells (That I can manipulate in storyboards) to my ViewController ? 那么,如何将静态单元格 (我可以在故事板中操作)的表格视图添加到我的ViewController

Here is what you can do. 这是你可以做的。 In your storyboard, create a parent view controller that contains all your non tableview views also, create a UITableViewController. 在故事板中,创建一个包含所有非tableview视图的父视图控制器,创建一个UITableViewController。 In your parent view controller, create container view, delete the view controller it auto adds, and right click and drag from the container view to your UITableViewController to create an embed segue. 在父视图控制器中,创建容器视图,删除它自动添加的视图控制器,右键单击并从容器视图拖动到UITableViewController以创建嵌入segue。 Your end result should look something like this: 您的最终结果应如下所示:

在此输入图像描述

You still need to do a couple of things: 你还需要做几件事:
Add <UITableViewDataSource, UITableViewDelegate> to your @interface declaration. <UITableViewDataSource, UITableViewDelegate>添加到@interface声明中。
Then you can set these also in Interface Builder. 然后您也可以在Interface Builder中设置它们。
Implement cellForRowAtIndexPath and call the dequeueReusableCellWithIdentifier method to return the cell. 实现 cellForRowAtIndexPath并调用 dequeueReusableCellWithIdentifier方法以返回单元格。

Sorry, I was wrong. 对不起我错了。 The truth is you cannot use static cells without a UITableViewController . 事实是,如果没有UITableViewController你就无法使用静态单元格。 Sorry. 抱歉。

A solution could be that you create two controllers and just add the view of the table view controller to your other view controller. 解决方案可能是您创建两个控制器,只需将表视图控制器的view添加到其他视图控制器。

As far as I know, you can't do this directly. 据我所知,你不能直接这样做。 At least in iOS 6, you had to use a UITableViewController when using static cells. 至少在iOS 6中,使用静态单元格时必须使用UITableViewController。 One way to use a static table view inside a UIViewController would be to add a container view in IB, and make the embedded controller a table view controller (delete the UIViewController you get automatically, drag in a UITableViewController, and hook it up with the embed segue). 在UIViewController中使用静态表视图的一种方法是在IB中添加容器视图,并使嵌入式控制器成为表视图控制器(删除自动获取的UIViewController,在UITableViewController中拖动,并将其与嵌入器连接起来Segue公司)。 You can get a reference to this table view controller from the UIViewController by implementing prepareForSegue:sender:, and using the destinationViewController property (which will point to the table view controller). 您可以通过实现prepareForSegue:sender:和使用destinationViewController属性(将指向表视图控制器)从UIViewController获取对此表视图控制器的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM