views:

157

answers:

3

I have a small cocoa app, and the UI consists mainly of a single NSTableView. However I have 2 different lists of data that I would like to display in it, and then just toggle between the two. My question is, what do you think is the best way to implement this?

Now I figure I could use a BOOL flag to change which Array gets used in the dataSource methods. But I would also need to change the NSCell class that is used because the lists contain completely different data objects. Then I would need to reload the table (would [table reloadData] reload all this reliably?).

Or, I could create 2 seperate NSTableViews in my NIB file and toggle their visibility... But this seems hackish.

I have a pretty good understanding of Cocoa, but I'm not really sure how to search for something like this, and I'm curious how other more experienced devs would solve this problem.

Thanks.

A: 

Why not have separate implementations of UITableViewDataSource and UITableViewDelegate, and change which ones the UITableView points to when the toggle event happens? Seems like it'd be a lot cleaner than having all that conditional code in one implementation. Just because most examples show the UITableViewController implementing both of those protocols doesn't mean they can't be three separate objects.

Sixten Otto
UITableView is not NSTableView.
Peter Hosey
Oh, geez. You're absolutely right, Peter. Way to fail reading comprehension on my part. Sorry, nick!
Sixten Otto
On the other hand, would the same principle not apply with the AppKit classes? It looks like they have the same basic structure of view, datasource, and delegate that the UIKit classes do? Are the parts of the machinery more closely bound?
Sixten Otto
In UIKit, data sources provide cells (which are views), and the table view doesn't have columns. In AppKit, each column has a single cell (which is not a view), and the table view uses that one cell for that column for every row. The data source provides only data, which the table view feeds to each column's cell, so switching the data source won't change the set of columns or their cell(s).
Peter Hosey
A: 

Sixten Otto's answer is the right one. That said, if I just wanted to hack together something that worked, I might do something like this:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (dataSourceOne) {
    [self cellForRowAtIndexPathDS1:indexPath];
  }
  else {
    [self cellForRowAtIndexPathDS2:indexPath];
  }
}

and then implement two versions of cellForRowAtIndexPath. I'm pretty sure the reload data method will reload everything on screen.

Hackish, but workable.

Kenny Winker
No, Sixten Otto's UITableView answer is not the right answer to an NSTableView question.
Peter Hosey
Oh right... not all cocoa questions are iPhone questions. Whooops!
Kenny Winker
+1  A: 

Or, I could create 2 seperate NSTableViews in my NIB file and toggle their visibility... But this seems hackish.

It's not hackish at all. It is hackish to use the same table view for multiple sets of disparate data.

Just create a tabless NSTabView with two tabs, put one NSTableView in the first tab and another NSTableView in the second tab. You can then switch between your table views by calling -selectTabViewItemAtIndex: on the NSTabView.

Rob Keniger
This is actually the solution I was hoping for. I still am learning what UI Controls are available to me in the Cocoa Framework, and a tabless "TabView" (or ViewStack as it's called in other frameworks) is exactly what I wanted to use. Thanks for pointing me to this.
nick