lele0108
lele0108

Reputation: 197

Change UITableView to UIView in Storyboards

I made the mistake of using a UITableView as the main controller of a view in Storyboards so now I want to convert it from a UITableVie to a UIView with a UITableView inside of it.

The problem is, well, XCode isn't a big fan of what I want to do. Inside my UITableView controller I changed the interface from UITableView to UIViewController but it's still no bueno inside of StoryBoard.

Storyboard refuses to let me drag a UIView into my view and put the TableView inside of it.

Any idea how to do this without scrapping everything?

Thanks!

Upvotes: 2

Views: 1454

Answers (2)

samthui7
samthui7

Reputation: 943

Some configs of storyboard file can be edited only when you open it in text format. For example, even when I changed the type of ViewController from UITableViewController to UIViewController in .storyboard file, and edited its .h + .m files, but the icon in storyboard was still a tableview enter image description here but not a view enter image description here, and I still couldnot add items outside the table, too.

I followed this tutor, and it worked well for me: http://steve.zazeski.com/convert-a-uitableviewcontroller-to-a-uiviewcontroller/.

In addition, if you want to keep the tableView in your class's view, just create a "subviews" tag, then copy & paste the tableView's xml code into its body. It will look like this:

<view key="view" contentMode="scaleToFill" id="qrH-Fc-mM8">
                        <rect key="frame" x="0.0" y="0.0" width="1000" height="600"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="BOG-Ip-6do">  *** content of your tableView ***</tableView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                </view>

Upvotes: 0

Mike S
Mike S

Reputation: 11419

You can simply copy and paste your UITableView from the UITableViewController to a new UIViewController:

Then you need to create a new custom UIViewController class file (to replace your UITableViewController) and copy and paste the programmatic control functions. You will also need to assign the proper delegates to the new UIViewController <UITableViewDataSource, UITableViewDelegate>. In the viewDidLoad function you must assign the new controller as the the UITableView's delegate and datasource. If that is not clear you can find many tutorials online to explain the details.

Upvotes: 4

Related Questions