Autolayout with TableViewController - xcode

I have my DetailViewController that I originally based on a UIViewController. The view uses Table View with static cells, but xcode then started to complain that I should use UITableViewController as a base class for the view. So I changed my view to be subclass of UITableViewController and wired everything again. There is a Date Picker in the bottom and when I start to use Autolayout to set constrains, xcode does not allow me to manage these under TVC. My setup works fine under ViewController.
Are there any limitations with Autolayout under TableViewController? I can also see that Table View always fills up whole view under TVC and does not allow uder subviews to exist on the same level.
Many thanks!

Only the UITableViewController can use static cells defined in the storyboard. However, it can only really show a table view, not other views as well - it's view property is the table view, which you can't just add new subviews to.
The solution is to use a table view controller to hold and manage the table, and to make this a child view controller of your main controller. This is trivial to achieve in storyboards using a container view and embed . The table view is then a subview of your main view controller's view, and you can add additional views such as the picker just as you were before.

Related

How to set view outlet for a UITableViewController?

I created an empty xib, I dragged a UITableViewController, set Custom class to my class that extends from UITableViewController at both the UITableViewController and File's Owner.
When I run, it says:
'A view can only be associated with at most one view controller at a time! View ; layer = ; contentOffset: {0, 0}> is associated with . Clear this association before associating this view with .'
At connections inspector I see that the UITableViewController element has:
tableView - Table View
view - Table View (greyed)
dataSource - Table View
delegate - Table View
First Responder has nothing.
File's Owner has:
tableView is not attached to anything
view - Table View
If I remove the view connection at File's Owner, it claims that the view outlet is not set.
It is difficult to imagine what happens based on the information provided. However:
The error message says that you have a UIView, and you try at runtime to connect this UIView to two UIViewControllers, which is not possible.
If you just subclass a UITableViewController, without adding properties or implementing methods, then setup a UITableViewController in an empty storyboard, and set the class of this UITableViewController to your subclass, you have only a single UITableViewController, and you will not get this error.
So somewhere in your project you must instantiate a second UITableViewController programmatically, and set its UIView property (which is a UITableView) to the UIView of the other UITableViewController.
You should ensure that you have only a single UITableViewController, namely your custom subclass.
I guess the correct answer is DON'T drag UITableViewController to the interface builder. Have 2 IBOutlets connected, one for the view, one for the table. DO drag a UITableView instead.
Hope this helps other people.

Create tables with mixed content

I must create a table with five rows. The first row contains a label and switch button, the remaining 4 rows contain simply a label.
This table has not to be modified.
Then, I used a storyboard to design table as specified above; now, I need a function to manage user's pressure on any row. Anyway, if I associate an UITableViewController class to my storyboard and run simulator, the table appears empty, because the methods of controller to fill rows have not been implemented. But I don't want to implement them, since my table has already been designed through the storyboard.
How can I design a controller simply managing my storyboard as it is, without defining functions trying to modify the structure I designed, but allowing to handle user's interactions as well?
If I loaded my storyboard into an UITableViewController through the use of function instantiateViewControllerWithIdentifier, would I have my table as I designed it, giving labels/table/button an identifier? How could I manage user's interaction in this case? how could I access the table and its field/row numbers?
Alternatively, how could I create my table programmatically, with a label and a button on first row?
If you need to easily use the standard switch cells, label cells, and other standard UI cells, then I'd strongly recommend you check out the free Sensible TableView framework. Should save you a ton of time.
The tableview you designed with storyboard has a custom class, it's your view controller. Even you've finished the design of your tableview with storyboard, you still have to implement the delegate & datasource methods in your view controller. In your case, you need to implement "tableView:numberOfRowsInSection" to return 5, and "tableView:heightForRowAtIndexPath" if your custom cells have different height.
You should create custom tableview cells to manage user's interaction. Design the cells in storyboard, implement the logic in your custom UITableViewCell classes. Set the cell's custom class to your custom UITableViewCell classes. Pass users' interaction events to your UITableViewController with delegate.
Create UITableView programmatically is not very different. Create an UITableView and add it as subview. Make sure you set self as the delegate and datasource of the tableview. Implement all the delegate & datasource methods in your ViewController. Create a custom UITableViewCell with button and label programmatically, and return it in "tableView:cellForRowAtIndexPath" when "indexPath.row == 0"

Xcode Table View sections without using TableViewController

I have a special thing to realize here.
I have a ViewController with a View on it. On the View I have an ImageView (I need a nice Background-Image) and many Buttons and Textfields and so on.
Now I need a Tableview. This Table view is located in the center of the screen.
I need sections in this tableview, so when I edit it to have sections its complaining,
because he wants a TableViewController to use static cells and sections.
Here's the problem. I cant use a TableViewController cause I need this stuff around
the tableview (those buttons and textfields) and I also need the image in the background.
So how can I realize that?
Interface Builder doesn't allow static cells in a table view that's embedded in a view controller. You need to have the table view's datasource point to your UIViewController. The view controller should hardcode the values/configuration for the cells. It's essentially the same thing... you just don't see the results until you run the app.
Also see this question Using static cells in a storyboard UIView with Xcode 4.2

Replace subview of NSSplitview with custom view

I still have a lot to learn with cocoa so I may have missed something obvious here. I have a custom view I would like to display in an nssplitview which replaces the current subview there.
I have a MessageView.xib file, and a MessageView .h/.m which subclasses NSView. I created a custom view instance for my main window (the one which contains the nssplitview) through Xcode 4's built in gui builder. I created an outlet to this instance of MessageView in my window's controller.
In my controller for the window when I want to swap out the subview for the splitview it runs this
[splitView replaceSubview:[[splitView subviews] objectAtIndex:1] with:viewMessage];
viewMessage is the outlet to the MessageView.
When this code is run the display of that subview changes to be blank. I'm not sure if there is something wrong with my custom view or there is some size issue. Is there something I need to do to fit the view into the split screen view or is my custom view just not displaying correctly? I have had a difficult time finding a tutorial on creating custom subviews with Xcode 4 so I'm not sure if something could be wrong with that. The custom view just has a label and a textfield in it.
Generally, you shouldn't need to replace NSSplitView's subviews with your own. Rather, you add your own custom view(s) as child views of the default subviews on each side of the divider. You can do this in code with addSubview:, but it's probably easier to just use Interface Builder in Xcode. Drag a "Custom View" into the splitview, then in the Identity Inspector, under Custom class, change the class to the name of your custom NSView subclass:
I think (off the top of my head, not tested), if you really do need to replace the default NSSplitView subviews with your own class, you can probably do it in Interface Builder using this same method, but selecting the default subview itself and changing its class in the inspector. This doesn't work for all AppKit classes, but it may work for NSSplitView.

How to set size of UITableView inside Interface Builder?

Is it possible to change the frame of UITableViewController's UITableView frame inside Interface Builder? I want to make my table, which I instantiate in IB, a little bit narrower.
When you have create your tableViewController in IB, if you then go to the section on the right of the screen where all the options are set size to "freeform" then drag it to whatever you like. When you init it it will then get this frame. Unless it is within a container controller.
The best way to do what you are after is to create a viewController and add a table view to its view. Make the table view narrower that the main controller view and wire it up to an IBOutlet on your controller. If you then make your controller implement the and methods you will be able to use your table view exactly as you want to.
Hope this is of help to you, if this is unclear or you would like any extra help, let me know :)
You can change it in IB but unless you customize your view controller somehow, it will set it be full screen (module enclosing controllers like nav, tab bar, split view, etc.) That's just the way view controllers work.
From the Apple docs:
When a view controller is displayed on screen, its root view is
typically resized to fit the available space, which can vary depending
on the window’s current orientation and the presence of other
interface elements such as the status bar.
You can create another UIView to hold your table view and then have more control over the table view sizing, But then you won't be a table view controller and will have to implement some of the things that table view controller provides you, e.g., deselectRowAtIndexPath on viewDidAppear.

Resources