PDA

View Full Version : Two TableViews in the one Window - Datasource??




Fritzables
Jun 21, 2011, 02:53 AM
Hi All,

Well, thanks to the people who helped out on my last question, I have finally worked out how to use the NSTableView and loading it with data from different arrays.

OK... now my window needs to have an additional TableView.

The issue is, the Class won't allow me to have two data source's:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)inputView {
return [inputArray count];
}

- (id)tableView:(NSTableView *)inputView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
return [inputArray objectAtIndex:rowIndex];
}

How do I now get around this?

Pete



jiminaus
Jun 21, 2011, 03:10 AM
The bad approach.


- (NSInteger)numberOfRowsInTableView:(NSTableView *)inputView {
if (inputView == firstTable) {
return [inputArray count];
} else if (inputView == secondTable) {
return [secondArray count];
} else {
// TODO: Throw exception
}
}

// (etc...)


The better approach is to have 2 view controllers, one for each table.

Fritzables
Jun 21, 2011, 03:17 AM
G'Day jiminaus,

Hmmm, I was thinking that but thought it was silly..... obviously not.

Thanks for the tip.

Pete

jiminaus
Jun 21, 2011, 03:20 AM
G'Day jiminaus,

Hmmm, I was thinking that but thought it was silly..... obviously not.

Thanks for the tip.

Pete

G'day Pete (nice to be writing to a fellow Aussie BTW).

No, it's not silly at all. It's the usual pattern. It's not that uncommon to have a window controller and then multiple view controllers. Especially if a view controller can be reused in multiple windows. Or in your case, being able to dynamically swap view controllers in and out.

Jim

chown33
Jun 21, 2011, 10:46 AM
Hmmm, I was thinking that but thought it was silly..... obviously not.


The data source doesn't have to be a view controller. Sometimes all that's needed is a data source for a table view, not an entire view controller for it.

A single view controller can contain two (or more) data source objects. This would only make sense if the view being controlled had two or more table views. So simply define two or more classes that implement the required protocol, and instantiate them in the view controller.