Change a UIViewController to a UITableViewControll

2019-02-08 07:43发布

I've made a view in my storyboard which I've now decided I'd rather display its data via static table cells.

I can't use static table views in a UIViewController (Static table views are only valid when embedded in UITableViewController instances). So, I need to convert my existing UIViewController to a UITableViewController somehow. I've changed the .h file's parent, but that hasn't done it.

Is there another way to get this going? I'd really rather not have to make a new VC in the storyboard and move everything over, it's a big hassle.

3条回答
We Are One
2楼-- · 2019-02-08 08:33

If you want your static cell table view not to take up the entire screen, then using a container view is the easiest way to go. Start with a regular UIViewController and drag a container view (next to normal UIView in the object list) into its view. Resize it however you want -- the storyboard will automatically provide a view controller connected to this container view with an embed segue. Delete that controller, drag out a table view controller and right-drag from the container view to this table view controller to make a new embed segue. This table view controller can be accessed from the UIViewController with its childViewControllers property (and conversely, you can access the UIViewController from the table view controller with parentViewController if you need to).

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-08 08:33

I'll add to this, since the question is about how to change a UIViewController into a UITableViewController, and given that this question is over a year old and the original answer, while valid and may or may not have been the only solution at the time, doesn't actually answer the question and is not the only solution.

It IS possible to do this, you just have to set up the table view delegate and datasource outlets in IB, and manually edit the storyboard XML, which sounds scary but is actually very easy.

First, change your class's parent to be a UITableViewController. UITableViewController already adopts the UITableViewDatasource and UITableViewDelegate protocols, so if your class does too you can remove them:

@implementation MyTableViewController : UITableViewController
...
@end

Next, create new referencing outlets on your UITableView for its dataSource and delegate. The easiest way to do this is to control-drag from the UITableView to itself. The popup will give you the dataSource and delegate options.

Lastly, you need to change the storyboard XML. The storyboard file can get pretty big pretty fast. The easiest way to find the scene you are looking for is by setting Storyboard Identifier in the Identity Inspector. To view the XML directly, right click on the storyboard file in the project navigator and select "Open As -> Source Code". Now just search for whatever you set the reuse identifier to earlier. You'll see something similar to this:

<!-- My Table View Controller -->
<scene sceneID="EuE-XX-cCb">
  <objects>
    <viewController storyboardIdentifier="MY_TABLE_VIEW_IDENTIFIER" ... >
      // Lots of other stuff
    </viewController>
  </objects>
</scene>

All you need to do is change the opening and closing view controller tags

<viewController>
</viewController>

to be tableViewController instead

<tableViewController>
</tableViewController>

That's it! No need to create a new UITableViewController scene or embed a UITableViewController in a container view.

EDIT:

I should also add that the UITableView MUST be the root view. It cannot be embedded inside another UIView.

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-08 08:43

What I did, is creating a UITableViewController in IB, open the Storyboard with a text editor, and copy all the nodes inside from the UIViewController to the UITableViewController.

I think that with this way there's less risk of deleting something important.

Before copying the sections objects, make sure that both tableviews (UIViewController and UITableViewController) have the same properties set like: static or dynamic cells, style (plain or grouped), etc.

查看更多
登录 后发表回答