What's the official way to load two tableview controllers on the same view controller?

The way I'm currently achieving this is by using the storyboard to point to one of the tableview controllers as both the datasource and delegate. I then use the viewDidLoad method to set the datasource and delegate for the other tableview controller:


self.tableView2.dataSource = self

self.tableView2.delegate = self


Is this how I should be acheiving this?

Replies

Do you really mean two table view controllers...or two table views?

Each tableview (NSTableView) should point to the viewController as its data source and delegate.


Each table view has an IBOutlet.


Then , in each tableView function, you have to test which tableview it is.


For instance :


    func numberOfRowsInTableView(tableView: NSTableView) -> Int {

        if tableView === firstTableView {
            return number of first tableview
        } else if tableView === secondTableView {    // second table view
            retur number of second tableview        }
    }



Do the same for other delegate functions :


func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {


and maybe for


func tableViewSelectionDidChange(notification: NSNotification) {

Do you really mean two table view controllers...or two table views?

That’s a really important question. Depending on how your data is structured, it might make more sense to have two separate table view controllers and embed them within your overall view controller.

Alternatively, if the data is tightly coupled than having one view controller managing two table views might make sense. If you do that then I recommend that you avoid the “test which table view you’re dealing with” approach on each delegate / data source callback, but instead have helper objects that take care of that and then wire those helpers up in code. Your helpers can then expose a nicer delegate interface back to your view controller.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 5 Jun through to Fri, 9 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

This, except I assign a tag to each table and use a switch statement

I am asking about two table views within the same view controller. I want to be able to click on a row in one table view and that changes what is displayed in the other table view. Claude- I tried wiring up the data source and delegates for each table view via the story board but when I do so, the delegate functions only get called on one of the table views. For example tableview.reloaddata() will only work for one of the tableviews but not the other one. When I wire one up via storyboard and the other I set programmatically via ViewDidLoad, they both work.

I noticed that in some cases, itr's better to control drag in IB from the tableview (not the bordered scrollview neither the clipView) to file owner's and then select delegate ; drag a second time and select data source.


Repeat for the second tableView.


Did you implement the test in the delegate functions ?


if tableView === firstTableView {

return number of first tableview

} else if tableView === secondTableView { // second table view

retur number of second tableview }

>I am asking about two table views within the same view controller.


Speaking of official ways...sounds like a SVC...