Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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'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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top