Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
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:

Code:
- (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

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
The bad approach.

Code:
- (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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.